Skip to content

Instantly share code, notes, and snippets.

@getify
Created June 27, 2012 06:44
Show Gist options
  • Save getify/3002042 to your computer and use it in GitHub Desktop.
Save getify/3002042 to your computer and use it in GitHub Desktop.
setTimeout vs. nested-RAF
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
})();
<!DOCTYPE html>
<html>
<head>
<style>
#foobar { position:absolute; left:0px; top:0px; width:200px; height:75px; background-color:#aaa;
-moz-transition: -moz-transform 0.3s; -moz-transform: translate(200px, 5px) translateZ(0px) scale(1, 1); -moz-transform-origin: 0px 0px;
-webkit-transition: -webkit-transform 0.3s; -webkit-transform: translate(200px, 5px) translateZ(0px) scale(1, 1); -webkit-transform-origin: 0px 0px;
}
#foobar.hidden {
-moz-transition: -moz-transform 0.3s; -moz-transform: translate(200px, -100px) translateZ(0px) scale(1, 1); -moz-transform-origin: 0px 0px;
-webkit-transition: -webkit-transform 0.3s; -webkit-transform: translate(200px, -100px) translateZ(0px) scale(1, 1); -webkit-transform-origin: 0px 0px;
}
</style>
</head>
<body>
<div id="foobar"></div>
</body>
</html>
function hide() {
var foobar = document.getElementById("foobar");
foobar.className = "hidden";
foobar.addEventListener("transitionend",do_hide,true);
foobar.addEventListener("webkitTransitionEnd",do_hide,true);
}
function do_hide() {
var foobar = document.getElementById("foobar");
foobar.style.display = "none";
foobar.removeEventListener("transitionend",do_hide,true);
foobar.removeEventListener("webkitTransitionEnd",do_hide,true);
}
// ****************************************
// this is ugly!
function show_1() {
var foobar = document.getElementById("foobar");
foobar.style.display = "block";
setTimeout(function(){
foobar.className = ""; // remove 'hidden' class
},50);
}
// BUT, is this actually better, or worse?
function show_2() {
var foobar = document.getElementById("foobar");
foobar.style.display = "block";
requestAnimationFrame(function(){
requestAnimationFrame(function(){
foobar.className = ""; // remove 'hidden' class
});
});
}
@hangy233
Copy link

Hi,
this code works great, but I'm confused about the 'requestAnimationFrame'.
Could you explain why nesting a raf in another?
Thanks

@piehei
Copy link

piehei commented Feb 14, 2018

For future reference, if someone else comes here and is not sure why the nested rAF: it's because single rAF call would schedule the layout update for the current frame while the second rAF call inside the first one schedules the layout update for the next frame. The layout will be repainted between frames so in order to have the display = "block" happen before (on the previous frame) the className = "", nesting is needed. Otherwise both of the changes would happen on the same frame.

@getify
Copy link
Author

getify commented Feb 14, 2018

@piehei +1 yep! :)

@kstratis
Copy link

kstratis commented Jun 3, 2022

@piehei +1! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment