Alternatively to the above suggestion, you could just use setInterval and let the browser worry about what it does to deliver events at correct times.
I think that is the best solution, actually.