Quick Summary of how JavaScript Timers Work
There are two functions that browsers expose to the JavaScript environment for asynchronously executing functions after a a specified amount of time in milliseconds: window.setTimeout and window.setInterval. Timeouts occur once, intervals repeat. However, JavaScript is single-threaded (with the exception of Web Workers) and so if a timer is set, but then the process blocks, the callback can be delayed or even fail to ever be fired.
The thread within which the JavaScript interpreter runs is also shared by the browser's UI and rendering systems. This means that not only will the above message never be logged, but that the browser will be unresponsive and appear to be frozen to the user.
It is possible to set so many timers expiring at about the same time that they just keep queuing up one after another; never giving the browser's UI a moment to update. I am going to refer to this as "timer congestion". I first read about this phenomena in Nicholas C. Zakas' book High Performance JavaScript (HPJS from now on). It was mentioned only in passing, however I found the idea fascinating. HPJS refers to research conducted by Neil Thomas while he was working on the mobile version of GMail.
Neil shared his research and the process that led him to do that research in his article Using Timers Effectively.
Introducing Chronos
Chronos is an implementation of the techniques described above which also mirrors the HTML 5 WindowTimers interface.
interface WindowTimers { long setTimeout(in any handler, in optional any timeout, in any... args); void clearTimeout(in long handle); long setInterval(in any handler, in optional any timeout, in any... args); void clearInterval(in long handle); };
This means that to switch existing code from using window.setTimout and friends, all you need to do is include Chronos on the page and replace window.setTimeout with chronos.setTimeout, etc to take advantage of what Chronos has to offer.
References
Using Timers Effectively by Neil Thomas
High Performance JavaScript by Nicholas C. Zakas
WHATWG HTML 5 Specification for Timers
How JavaScript Timers Work by John Resig
Understanding JavaScript Timers by Angus Croll
setTimeout Patterns in JavaScript by Yours Truly















