I made a goog.Timer object (http://closure-library.googlecode.com/svn/docs/class_goog_Timer.html) with new goog.Timer(1) to run a function every millisecond via listening to the tick event. However, the function seemed to be running every 100 milliseconds instead.
I hypothesized that my function was taking a while to run (and javascript is single-threaded of course), so it took a while to get to the next round. Hence, I set the interval for the timer to 100, and it reliably ran every 1/10 of a second.
Does The Google Closure library have a more reliable timer that only runs a function precisely at the interval? If there is not enough time to run a function in one cycle, I am fine with canceling the previous call and running it the next time a tick is dispatched.
Nicholas Zakas wrote up a nice summary of Timer resolution in browsers on his blog. As Nicholas pointed out, the HTML5 timers specification (as of August 2, 2012) dictates that the minimum interval for
setTimeout()andsetInterval()is 4 milliseconds.I wrote the following demo application to test the minimum interval delay for
goog.Timer.Running this program in Chrome version 21 consistently shows approximately 4.2 milliseconds per
goog.Timertick event, which is very close to the minimum allowable browser timer resolution of 4 milliseconds.