3

Is it possible to update in intervals similar to 10nths of milliseconds. I used

window.setInterval(timeUpdate,20);

Is this possible or does the browser's thread have some sort of minimum period?

Thanks

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Potney Switters
  • 2,902
  • 4
  • 33
  • 51
  • 1
    Note: this is JavaScript, and has nothing to do with jQuery (which is a library written *in* JavaScript) – Piskvor left the building May 03 '12 at 12:42
  • my bad! I know the difference. I mixed the javascript and jQuery code thus the confusion. Thanks for the correction :) – Potney Switters May 03 '12 at 12:44
  • 1
    http://stackoverflow.com/questions/4870569/javascript-countdown-with-showing-milliseconds you might find this helpful or may be this http://stackoverflow.com/questions/2604450/how-to-create-a-jquery-clock-timer – Devjosh May 03 '12 at 12:47

2 Answers2

3

Depending on the browser, it's version and if there's focus on the tab, the timer resolution may differ. For more detail please have a look at the following refrence, especially the Minimum delay and timeout nesting part.

Daniel Baulig
  • 10,739
  • 6
  • 44
  • 43
  • Thanks Daniel. Xmmm the document explains that if I set the time interval of the event to 1 ms for example, the browser would cast it down to the least possible(4-10ms depending on the the things you mentioned). This is really weird because I am trying to move a small image across a div and and if that was true the whole moving of the image should flow and and not act like a moving frame. – Potney Switters May 03 '12 at 12:52
  • What do you mean with "if that was true the whole moving of the image should flow and not act like a moving frame"? – Daniel Baulig May 03 '12 at 12:53
  • I mean it is should be true, since it is specs but the behavior I get does not match with that. I am not sure that the problem roots to the event occurence. I need to check that the event is actually triggered in very small time intervals – Potney Switters May 03 '12 at 13:15
  • Potney Switters: Or, the JS engine in the browser, on that specific computer, may not be capable of responding to the events quickly enough as they come in. – Piskvor left the building May 03 '12 at 14:10
  • Yes that could also be the case, thanks for the comment Piskvor. It seems that there was another event that was not updated soon enough, this way misleading me for the timings. I set the interval of the initial code present to 0 and is close to a continuous loop. Thanks everyone for the specifics :) – Potney Switters May 03 '12 at 14:52
1

It is possible to set a very small interval (1 ms, for example); however, most browsers have a minimal timer granularity, usually somewhere between 4-50 ms; special cases may apply for inactive browser views/tabs/windows.

So: the function call is correct, and will complete successfully, but don't expect that you'll actually get that very short resolution - first, the browser may not give it to you, and second, the computer may not be fast enough to process the events at that speed, even if the event fires that quickly.

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222