Today, I was making a loop with a setTimeout(). Unfortunately, some function parameters were getting strange values.
In short, this is what’s happening:
var x = 1;
var steps = 3;
var timer = false;
function myFunc( y ){
if( !isNaN(y)&&parseInt(y)==y&&y>0 ) { // if y is int and greater than 0
x = y;
} else { // y is no int or is below 0
if( x >= steps ) { // x is greater than or equal to steps, return to first step
x = 1
} else { // x is less than steps, add 1
x++;
}
}
window.clearTimeout( timer );
timer = setTimeout( myFunc, 1000 );
}
Now, somehow Mr. Firefox is randomly throwing an int, far greater than “steps” into the “y” parameter while triggering the timer… WHY does it do that?
I’ve resolved this problem by simple doing this:
timer = setTimeout( function(){ myFunc( -1 ) }, 1000 );
But still… WHY was the browser giving random numbers in the “y” parameter?
Anyone?
Firefox will call the function with the number of milliseconds “late” the function is in executing.