Skip to content

Instantly share code, notes, and snippets.

@patrinhani-ciandt
Created November 8, 2016 04:47
Show Gist options
  • Save patrinhani-ciandt/a2fbc165dcb5719d800c72fd2fcbb031 to your computer and use it in GitHub Desktop.
Save patrinhani-ciandt/a2fbc165dcb5719d800c72fd2fcbb031 to your computer and use it in GitHub Desktop.
Request animation replacer to use when browser tab is unselected and you need to avoid to page load waiting to tab get focus
(function(root) {
var originalRAF = root.requestAnimationFrame;
var customRAF = function (cb) {
return setTimeout(cb, 1000 / 60);
};
var useOriginal = false;
//overwrite the function only once because other components also overwrite it
//this way, no one will have directly reference to customRAF
root.requestAnimationFrame = function(cb) {
return useOriginal ? originalRAF(cb) : customRAF(cb);
};
originalRAF(function() {
useOriginal = true; //will be executed when page get focus
});
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment