Forums > MaxMSP

javascript timing

May 22, 2007 | 10:35 pm

Hello,
I realize from reading the docs and this forum that javascript is not for timing-critical tasks. However, I want to use for something where even 100 ms. off won’t make a difference. In particular, I want to have a series of processes which randomly update their internal timing. However, when I attempt to measure what the result is (using timer), I’m getting stuff that’s pretty far off. It’s entirely possible that I’m doing something stupid, or not grasping something. Anyway, the javascript (which wants to be in a file named "debug.js") and the patch follow. I was expecting the number boxes to display approximately the same numbers (which they don’t most of the time). thanks!, Michael

inlets = 1;
outlets = 1;

var rOffset = 3000;
var rRange = 2000;

var vcTsk = new Task(vcDo,this);
vcTsk.interval = 4000;

function vcDo()
{
outlet(0,vcTsk.interval);
vcTsk.interval = Math.round(Math.random()*rRange) + rOffset;
}

function msg_int(a){

if (a == 1){
vcTsk.repeat();
}
if (a == 0){
vcTsk.cancel();
}
}

max v2;
#N vpatcher 62 44 825 527;
#P window setfont "Sans Serif" 9.;
#P number 195 142 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 266 140 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P button 302 179 15 0;
#P button 266 178 15 0;
#P window linecount 1;
#P newex 266 201 35 196617 timer;
#P toggle 266 81 15 0;
#P newex 266 113 59 196617 js debug.js;
#P connect 2 0 6 0;
#P connect 1 0 0 0;
#P connect 0 0 5 0;
#P connect 5 0 3 0;
#P connect 3 0 2 0;
#P connect 4 0 2 1;
#P connect 5 0 4 0;
#P pop;


May 23, 2007 | 6:27 am

On 23 May 2007, at 00:35, Michael Theodore wrote:
> However, when I attempt to measure what the result is (using
> timer), I’m getting stuff that’s pretty far off. It’s entirely
> possible that I’m doing something stupid, or not grasping
> something. Anyway, the javascript (which wants to be in a file
> named "debug.js") and the patch follow. I was expecting the number
> boxes to display approximately the same numbers (which they don’t
> most of the time). thanks!, Michael

hi michael,
are you comparing the right numbers? maybe stick a [bucket] inbetween.
it seems to work fine over here.
volker.

#P window setfont "Sans Serif" 9.;
#P number 141 191 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window linecount 1;
#P newex 141 137 39 196617 bucket;
#P number 75 191 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 75 99 64 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P button 100 137 15 0;
#P button 75 137 15 0;
#P newex 75 160 35 196617 timer;
#P toggle 75 40 15 0;
#P newex 75 72 59 196617 js debug.js;
#P connect 5 0 3 0;
#P connect 5 0 4 0;
#P connect 5 0 7 0;
#P connect 7 0 8 0;
#P connect 2 0 6 0;
#P connect 1 0 0 0;
#P connect 0 0 5 0;
#P connect 3 0 2 0;
#P connect 4 0 2 1;
#P window clipboard copycount 9;


May 23, 2007 | 3:22 pm

Thanks very much, Volker! You’re absolutely right–I was one off, as bucket demonstrates. Much appreciated!
regards,
Michael


Viewing 3 posts - 1 through 3 (of 3 total)