Created
February 15, 2017 16:47
-
-
Save LiamChapman/db1e49f8213c3b91222fcbb126b915d1 to your computer and use it in GitHub Desktop.
RequestAnimationFrame and RequestTimeout
This file contains hidden or 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
| // @sources: paulirish & joelambert | |
| (function() { | |
| var lastTime = 0; | |
| var vendors = ['webkit', 'moz']; | |
| 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); | |
| }; | |
| /** | |
| * 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) { | |
| 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 = new Object(); | |
| function loop(){ | |
| var current = new Date().getTime(), | |
| delta = current - start; | |
| delta >= delay ? fn.call() : handle.value = requestAnimationFrame(loop); | |
| }; | |
| handle.value = requestAnimationFrame(loop); | |
| return handle; | |
| }; | |
| /** | |
| * Behaves the same as clearTimeout except uses cancelRequestAnimationFrame() where possible for better performance | |
| * @param {int|object} fn The callback function | |
| */ | |
| window.clearRequestTimeout = function(handle) { | |
| window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) : | |
| window.webkitCancelAnimationFrame ? window.webkitCancelAnimationFrame(handle.value) : | |
| window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value) : /* Support for legacy API */ | |
| window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) : | |
| window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) : | |
| window.msCancelRequestAnimationFrame ? window.msCancelRequestAnimationFrame(handle.value) : | |
| clearTimeout(handle); | |
| }; | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment