base ={time:0};
var loop = 0;
setInterval(function(){
if(base.time === 9000){
move();
base.time = 0;
}
base.time ++;
},1);
Shouldn’t the move(); function occur every 9s? I timed it and its much less, why is that?
setIntervalwill not run every millisecond. There is a minimum possible interval that is longer than that.If you want something to run in nine seconds, you should use
setTimeout()for 9 seconds. Plus your code doesn’t reset base.time back to zero so it would only match 9000 once anyway.If you want it to run every 9 seconds, then you can use
setInterval(handler, 9000)or you can usesetTimeout(handler, 9000)and then set the nextsetTimeoutin your handler function.This will execute
move()every nine seconds:Here’s a useful article on the topic: http://www.adequatelygood.com/2010/2/Minimum-Timer-Intervals-in-JavaScript.
To reset the time back to 9 seconds when a button is clicked use this code:
See it in action here: http://jsfiddle.net/jfriend00/sF2by/.