Jump to content
  • Advertisement
Sign in to follow this  
Awoken

setInterval

This topic is 502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm just wondering if anyone knows off the top of their head the answer to the following.

When I use setInterval( function(){ // do something // } , 150 ).  is the function executed once every 150 milliseconds like clock work, or is the function executed 150 milliseconds after the function is finished doing its work??  let's say the work in question take 300 milliseconds.  Does it go like:

1.  150 milli >> execute function

2.  +300 milli >> function finishes

3.  +0 milli >> execute function ( because 300 is greater than 150 ) OR

3.  + 150 milli >> execute function

Share this post


Link to post
Share on other sites
Advertisement

The truth is somewhere inbetween the two. :) setInterval afaik doesn't care about how long the passed callback runs, and it won't fire exactly on given delays either. With the given example, the browser will queue up the passed callback for execution every 150ms, but it will only execute when the execution "thread" doesn't have anything else important to run. (This is why the age old setTimeout with zero delay hack works, a 0 as a delay in this case doesn't mean it will execute immediately, it just means that it will run as soon as possible when nothing else is running.)

Sidenote: it is perfectly possible to do the second option you described with recursive setTimeouts, e.g:

(function func(){
    setTimeout(function(){
        //code
        func();
    }, delay);
})();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!