4 ms·
It doesn't actually have to know. There's a JavaScript API called requestAnimationFrame which asks the browser to invoke a callback for the next display refresh
by mikeash 12y ago
It doesn't actually have to know. There's a JavaScript API called requestAnimationFrame which asks the browser to invoke a callback for the next display refresh. Invoke requestAnimationFrame again from that callback, and you'll get a steady stream of callbacks at the display refresh rate, or whatever the browser thinks it is.
Here's the relevant code from the demo:
var work = (function() {
var toggle = false;
return function tester() {
frame1.style.opacity = toggle?.5:1;
frame2.style.opacity = toggle?1:.5;
toggle=!toggle;
requestAnimationFrame(tester);
};
})();
requestAnimationFrame(work);