0

Is there any alternative of setTimeout(callback,sleep_time_in_millisec) api in js where I can use sleep time in microsec or nanosec value;

or setTimeout(callback,sleep_time_in_millisec) itself is that much accurate to handle float value in sleep_time_in_millisec?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Scarecrow
  • 4,057
  • 3
  • 30
  • 56
  • 1
    The best some browsers will do is to respond in about 15ms increments, so setting a value in micro or nanoseconds is pointless. Also, there really isn't any standard for setTimeout (it's in [HTML5](http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers), but really only to document current behaviour) so not particularly reliable if precision or accuracy are required. – RobG Jul 06 '13 at 13:09
  • @RobG, Thanks for reply, may be minimum value is 15 ms or 10 ms; but I want to make it sleep for 30.5589 millisec ; not 30 millisec or 31 millisec... – Scarecrow Jul 06 '13 at 13:16
  • Note I wrote **about 15ms increments**, not *minimum value*, so what you want to do is impossible with common javascript implementations. – RobG Jul 07 '13 at 23:34

2 Answers2

4

There is no such API and setTimeout will not work with float values. In fact, setTimeout offers no guarantee that your callback will be invoked when the designated time has passed but only when at least as much as the designated time has passed (it could invoke the callback after one hour and that would still be OK according to the API specification).

See also What is minimum millisecond value of setTimeout?

If you are writing time-critical code, JavaScript is the wrong language to do it in.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks for reply, yes my code is time-critical; and I am writing it for browser; if javascript is not good language for my project, then what is your suggestion ? which language should I prefer? – Scarecrow Jul 06 '13 at 13:09
  • 1
    @Swarnendu: Well, it would take a native-based technology (ActiveX, Java applet, or Chrome [Native Client](https://developers.google.com/native-client/)). Except for Java these are of course not cross-browser. But my rule of thumb suggestion would be "just don't try to do it in the browser". – Jon Jul 06 '13 at 13:13
  • Well that is not a language limitation, `setTimeout` is not even part of the language. If there is need for such facilities in the browser, then you should request them/make your case. Slightly related is the addition of [high resolution time API](http://www.w3.org/TR/hr-time/). – Esailija Jul 06 '13 at 14:41
1

Have you try with setinterval(callback,sleep_time_in_millisec) and clearinterval in javascript

AbnSrn
  • 566
  • 2
  • 6
  • 20
  • does `setInterval` handles `float value` in sleep_time_in_millisec? – Scarecrow Jul 06 '13 at 13:11
  • Can you pls refer this link http://stackoverflow.com/questions/8468790/is-it-safe-to-pass-setinterval-or-settimeout-a-fractional-delay. I think its may helpful for you. – AbnSrn Jul 06 '13 at 13:23