Last active
August 29, 2015 14:16
-
-
Save rentes/3068e48364d9ad907124 to your computer and use it in GitHub Desktop.
Refactors setInterval(), clearInterval(), setTimeout(), clearTimeout() to use the requestAnimationFrame() method for performance purposes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* requestAnimationFrame() by Miguel Rentes | |
* Corrects all JSLint warnings and uses a browser-compatible requestAnimFrame() function | |
* Based on Paul Irish's work - http://paulirish.com/2011/requestanimationframe-for-smart-animating/ | |
* Also, please see https://gist.github.com/joelambert/1002116 | |
*/ | |
window.requestAnimFrame = (function () { | |
"use strict"; | |
return window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
window.oRequestAnimationFrame || | |
window.msRequestAnimationFrame || | |
function (callback) { | |
window.setTimeout(callback, 1000 / 60); | |
}; | |
}()); | |
/** | |
* Behaves the same as setInterval except uses requestAnimationFrame() where possible for better performance | |
* @param {function} fn The callback function | |
* @param {int} delay The delay in milliseconds | |
*/ | |
window.requestInterval = function (fn, delay) { | |
"use strict"; | |
if (!window.requestAnimationFrame && | |
!window.webkitRequestAnimationFrame && | |
!(window.mozRequestAnimationFrame && window.mozCancelRequestAnimationFrame) && // Firefox 5 ships without cancel support | |
!window.oRequestAnimationFrame && | |
!window.msRequestAnimationFrame) { | |
return window.setInterval(fn, delay); | |
} | |
var start = new Date().getTime(); | |
var handle = Object.create(null); | |
function loop() { | |
var current = new Date().getTime(); | |
var delta = current - start; | |
if (delta >= delay) { | |
fn.call(); | |
start = new Date().getTime(); | |
} | |
handle.value = window.requestAnimFrame(loop); | |
} | |
handle.value = window.requestAnimFrame(loop); | |
return handle; | |
}; | |
/** | |
* Behaves the same as clearInterval except uses cancelRequestAnimationFrame() where possible for better performance | |
* @param {int|object} handle The callback function | |
*/ | |
window.clearRequestInterval = function (handle) { | |
"use strict"; | |
if (window.cancelAnimationFrame) { | |
window.cancelAnimationFrame(handle.value); | |
} else if (window.webkitCancelAnimationFrame) { | |
window.webkitCancelAnimationFrame(handle.value); | |
} else if (window.webkitCancelRequestAnimationFrame) { | |
window.webkitCancelRequestAnimationFrame(handle.value); | |
} else if (window.mozCancelRequestAnimationFrame) { | |
window.mozCancelRequestAnimationFrame(handle.value); | |
} else if (window.oCancelRequestAnimationFrame) { | |
window.oCancelRequestAnimationFrame(handle.value); | |
} else if (window.msCancelRequestAnimationFrame) { | |
window.msCancelRequestAnimationFrame(handle.value); | |
} else { | |
clearInterval(handle); | |
} | |
}; | |
/** | |
* Behaves the same as setTimeout except uses requestAnimationFrame() where possible for better performance | |
* @param {function} fn The callback function | |
* @param {int} delay The delay in milliseconds | |
*/ | |
window.requestTimeout = function (fn, delay) { | |
"use strict"; | |
if (!window.requestAnimationFrame && !window.webkitRequestAnimationFrame && | |
!(window.mozRequestAnimationFrame && window.mozCancelRequestAnimationFrame) && // Firefox 5 ships without cancel support | |
!window.oRequestAnimationFrame && !window.msRequestAnimationFrame) { | |
return window.setTimeout(fn, delay); | |
} | |
var start = new Date().getTime(), | |
handle = Object.create(null); | |
function loop() { | |
var current = new Date().getTime(); | |
var delta = current - start; | |
if (delta >= delay) { | |
fn.call(); | |
} else { | |
handle.value = window.requestAnimFrame(loop); | |
} | |
} | |
handle.value = window.requestAnimFrame(loop); | |
return handle; | |
}; | |
/** | |
* Behaves the same as clearTimeout except uses cancelRequestAnimationFrame() where possible for better performance | |
* @param {int|object} handle The callback function | |
*/ | |
window.clearRequestTimeout = function (handle) { | |
"use strict"; | |
if (window.cancelAnimationFrame) { | |
window.cancelAnimationFrame(handle.value); | |
} else if (window.webkitCancelAnimationFrame) { | |
window.webkitCancelAnimationFrame(handle.value); | |
} else if (window.webkitCancelRequestAnimationFrame) { | |
window.webkitCancelRequestAnimationFrame(handle.value); | |
/* Support for legacy API */ | |
} else if (window.mozCancelRequestAnimationFrame) { | |
window.mozCancelRequestAnimationFrame(handle.value); | |
} else if (window.oCancelRequestAnimationFrame) { | |
window.oCancelRequestAnimationFrame(handle.value); | |
} else if (window.msCancelRequestAnimationFrame) { | |
window.msCancelRequestAnimationFrame(handle.value); | |
} else { | |
clearTimeout(handle); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment