-2

The time delay given in setTimeout() works differently for Windows and Ubuntu?

Why is that?

Is there a better way?

alex
  • 479,566
  • 201
  • 878
  • 984
kxj
  • 13
  • 4
  • 5
    Browser dependent not OS dependent – aaronman Jun 19 '13 at 05:44
  • @aaronman Had run the code in firefox in 2 different systems with windows and ubuntu OS. – kxj Jun 19 '13 at 05:47
  • 1
    what do you mean it works DIFFERENTLY ? what're you observing? the call theoretically doesn't work differently on different browsers NOR on different operating systems. – Ahmed Masud Jun 19 '13 at 05:47
  • @aaronman why do you think setTimeout behavior is browser dependent ? – Ahmed Masud Jun 19 '13 at 05:48
  • http://stackoverflow.com/q/196027/1283215 – Hussain Akhtar Wahid 'Ghouri' Jun 19 '13 at 05:48
  • @Ahmed Masud I had to put more time on windows to run the code whereas lesser time in ubuntu to run the code correctly. – kxj Jun 19 '13 at 05:49
  • 2
    setTimeout is not an accurate timer. @Hussain pointed out a very nice set of answers. look at the answer BELOW the accepted answer with there is a reference to this: http://www.sitepoint.com/creating-accurate-timers-in-javascript/ which may be something you want to use – Ahmed Masud Jun 19 '13 at 05:53
  • @AhmedMasud they are likely very similar but each browser has it's own javascript implementation – aaronman Jun 19 '13 at 06:13
  • From your comment above: "_I had to put more time on windows to run the code..._" This suggests to me that you might be using some kind of delay to "_get your code to work_." That's something you should never do. In JavaScript callback functions are generally used when you need to "_call function X_" after "_event Y has completed_". Google around about callback functions or open another question (or edit this one) with some simple example code to demonstrate what you're trying to do. – jahroy Jun 19 '13 at 07:34

2 Answers2

2

The time given in setTimeout() is not guaranteed.

There are several reasons for that:

  • The eventQueue system of JavaScript
  • Timer accuracy depending on the OS and/or browser (is between 1ms and 20ms)

I wrote a blog post about time in JavaScript at

http://www.codebullets.com/is-time-relative-in-javascript-1365 about this topic

Better way for what?

It depends what you want to do. Also take a look at RequestAnimationFrame (http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/), maybe this is for you.

chris
  • 111
  • 4
  • How JavaScript Timers Work, John Resig http://ejohn.org/blog/how-javascript-timers-work/ – chris Jun 19 '13 at 07:08
  • Timer resolution in browsers, Nicholas C. Zakas, http://www.nczonline.net/blog/2011/12/14/timer-resolution-in-browsers/ – chris Jun 19 '13 at 07:08
  • Timers in HTML5, http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers – chris Jun 19 '13 at 07:09
1

requestAnimationFrame will trigger for each monitor VBLANK gap if requested before the next VBLANK.

That way it is a very accurate timer. However, it triggers up to 60 times per seconds and it won't be very accurate if you spend more than the 16.7ms time-budget you get before you need to call it again.

If your functions operates within this time-budget, requestAnimationFrame will be most accurate you can get access to from Javascript.

It's triggered almost the same way as setTimeout:

function myLoop() {

    //... < 16.7ms to finish work...

    requestAnimationFrame(myLoop);
}

Note: you will need to use prefixes in some browsers or a polyfill (see chris' answer for link).

  • 1
    I'd use `requestAnimationFrame = requestAnimationFrame || prefixes || setTimeout` for a polyfill. – John Dvorak Jun 19 '13 at 07:25
  • 1
    `requestAnimationFrame` won't fire if the browser window/tab is not visible, will it? – John Dvorak Jun 19 '13 at 07:25
  • @JanDvorak it will, but at reduced rate (typical half or 1/30s).. –  Jun 19 '13 at 07:33
  • My guess is the OP is just using an arbitrary delay to get some code to work after something has loaded (or completed)... Maybe I'm wrong, but I don't think the actual precision of the timer is his issue. Of course it would be nice if he clarified something. – jahroy Jun 19 '13 at 07:40