Categories
josephscott

JavaScript Timing, Nothing Under 10ms

Tobie Langel pulled out an interesting little JavaScript nugget from the recent Background Music post on the Webkit/Safari Blog:

Some Web pages specify repeating timers with an extremely small timeout. In fact they often use the value 0 to mean “Fire as soon as you can.” Safari 2 does not throttle these timeouts, and so a poorly-constructed page that specifies a repeating timer under 10ms will actually hog a lot of CPU. Aggressive timers were actually a problem in Mozilla for a long time before being fixed, and they are currently a problem in Safari 2. WinIE, Firefox, and WebKit nightlies basically error-correct the badly constructed page and ignore timer values of < 10ms by changing them to be 10ms.

So don’t bother using setTimeout or setInterval for something less than 10ms.

2 replies on “JavaScript Timing, Nothing Under 10ms”

hello,
im a javascript newbie.

im currently developing an AJAX based chat system.

im really in doubt on what to use between setInterval and setTimeout to periodically checks for new messages and user’s that is currently online.

i’ve read that setTimeout is widely used..

so my question is which one do you think will be the best for me?

thanks

@Dan –

While the two are similar, they aren’t identical. Which one to use will depend on what exactly you need to have happen. setInterval will run until you tell it not to, setTimeout will run just once unless you reset it each time.

Leave a Reply

Your email address will not be published. Required fields are marked *