Domanda

Is there any diffrence in intervals of those functions?

    function myIntervalFunction() {
        for(var c=1000000000000;c>0;--c) ;
    }

    setInterval(myIntervalFunction,1000);

and

    function myIntervalFunction() {
    }

    setInterval(myIntervalFunction,1000);

So does setInterval pauses javaScript for given interval or does it triggers after exact period of milliseconds? After testing I think it takes longer if some code run, but I'm not sure. Could that be possible and if so how do i make it exact period?

Note: Instead of looping my code draws on a HTML5 canvas.

È stato utile?

Soluzione

The setInterval call will place your function in the callback queue for a timer but it's not guaranteed to hit the interval that you specify with great precision. Each JS engine implementation varies in how it implements the timers but most will probably have some sort of pooling of timer callbacks which means that it's hard to predict the exact variation between the desired interval and the actual interval between calls to your callback function.

And the resolution gets worse when you get down to the tens of milliseconds level.

If you're looking for a way to get constant frame rate for animation you may want to take a look at this post that discusses using the requestAnimationFrame API available in modern borwsers.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top