| // requestAnimationFrame() shim by Paul Irish | |
| // http://paulirish.com/2011/requestanimationframe-for-smart-animating/ | |
| window.requestAnimFrame = (function() { | |
| return window.requestAnimationFrame || | |
| window.webkitRequestAnimationFrame || | |
| window.mozRequestAnimationFrame || | |
| window.oRequestAnimationFrame || | |
| window.msRequestAnimationFrame || | |
| function(/* function */ callback, /* DOMElement */ element){ | |
| 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) { | |
| 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(), | |
| handle = new Object(); | |
| function loop() { | |
| var current = new Date().getTime(), | |
| delta = current - start; | |
| if(delta >= delay) { | |
| fn.call(); | |
| start = new Date().getTime(); | |
| } | |
| handle.value = requestAnimFrame(loop); | |
| }; | |
| handle.value = requestAnimFrame(loop); | |
| return handle; | |
| } | |
| /** | |
| * Behaves the same as clearInterval except uses cancelRequestAnimationFrame() where possible for better performance | |
| * @param {int|object} fn The callback function | |
| */ | |
| window.clearRequestInterval = 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) : | |
| 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) { | |
| 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 = requestAnimFrame(loop); | |
| }; | |
| handle.value = requestAnimFrame(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); | |
| }; |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
leeoniya
commented
Aug 12, 2011
|
are these shims also designed to work without Paul's window.requestAnimFrame? otherwise i see no point in duplicating the checks that paul already does in his. simply do |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
joelambert
Aug 15, 2011
The shims require Pauls requestAnimFrame() to call the correct vendor prefixed function which is why his code is also bundled. Also, Firefox 5 ships with "broken" support by only providing mozRequestAnimationFrame and not mozCancelRequestAnimationFrame. The checks are important to work around this until this is fixed by Mozilla.
|
The shims require Pauls requestAnimFrame() to call the correct vendor prefixed function which is why his code is also bundled. Also, Firefox 5 ships with "broken" support by only providing |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
timc3
Apr 30, 2012
Are these still working correctly with the changes to the API? I am basically doing something like this: and get mycallback never gets called.
var timer = window.requestInterval(mycallback, 60);
timc3
commented
Apr 30, 2012
|
Are these still working correctly with the changes to the API? I am basically doing something like this: and get mycallback never gets called. var timer = window.requestInterval(mycallback, 60); |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Glidias
Nov 1, 2012
Take note that this code will use a global "handle" variable. So, make sure you aren't using it in global namespace, or have to replace it with another name.
For window.requestInterval's loop() function, there is a mistake, which would cause clearRequestInterval() to not work (ie. loop will continue to run indefinitely) if canceling the frame happens to be done within the fn.call() invocation.
It might be better if fn.call() occurs after requestAnimFrame(), not before it. This would allow situations within fn.call() to cancel the latest requested animation frame id (which should be the next frame), and not end up canceling an outdated animation frame id (which is the current frame being executed).
Here it is edited function:
function loop() {
handle.value = requestAnimFrame(loop); // replace this in first line instead! request next frame.
var current = new Date().getTime(),
delta = current - start;
if(delta >= delay) {
fn.call();
start = new Date().getTime();
}
};
Glidias
commented
Nov 1, 2012
|
Take note that this code will use a global "handle" variable. So, make sure you aren't using it in global namespace, or have to replace it with another name. For window.requestInterval's loop() function, there is a mistake, which would cause clearRequestInterval() to not work (ie. loop will continue to run indefinitely) if canceling the frame happens to be done within the fn.call() invocation. It might be better if fn.call() occurs after requestAnimFrame(), not before it. This would allow situations within fn.call() to cancel the latest requested animation frame id (which should be the next frame), and not end up canceling an outdated animation frame id (which is the current frame being executed). Here it is edited function:
|
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
gmyx
May 23, 2013
@Glidias I've implemented your suggesting in my fork.
I've also changed requestTimeout to allow passing an argument with the function call.
gmyx
commented
May 23, 2013
|
@Glidias I've implemented your suggesting in my fork. I've also changed requestTimeout to allow passing an argument with the function call. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
webcss
Jan 17, 2014
In order for the requestAnimFrame shim to work in IE9 (and below), the setTimeout fallback should return its handle:
function(/* function / callback, / DOMElement */ element){
return window.setTimeout(callback, 1000 / 60);
};
webcss
commented
Jan 17, 2014
|
In order for the requestAnimFrame shim to work in IE9 (and below), the setTimeout fallback should return its handle: function(/* function / callback, / DOMElement */ element){ |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
MikeLP
Nov 10, 2014
Please help. Why this case not working?
var time = 1;
var r = requestInterval(function() {
console.log('tick', time, time == 15, r)
if(time == 15) {
clearRequestInterval(r);
}
time++
}, 1000)
But if you type in console clearRequestInterval(r); - this work?
MikeLP
commented
Nov 10, 2014
|
Please help. Why this case not working? var time = 1; }, 1000) But if you type in console clearRequestInterval(r); - this work? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
kopax
commented
Mar 16, 2016
|
Is this still usefull in 2016 ? I guess not |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
misbeliever
commented
Mar 23, 2016
|
@kopax why not? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
webvitalii
Jun 14, 2016
I guess code can be simplified by removing the preffixed code based on the current stats:
webvitalii
commented
Jun 14, 2016
•
|
I guess code can be simplified by removing the preffixed code based on the current stats: |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
hinell
commented
Dec 23, 2016
•
|
Can somebody create an npm package for it? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
englishextra
commented
May 29, 2017
|
clearRequestTimeout wont work as it is said above |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
L2L2L
Jan 26, 2018
you have param(int|obj) for both clear/cancel function, but it does not allow either a object or integer but just a object. You can put a test case to see rather the argument is a object or integer.
L2L2L
commented
Jan 26, 2018
|
you have param(int|obj) for both clear/cancel function, but it does not allow either a object or integer but just a object. You can put a test case to see rather the argument is a object or integer. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
L2L2L
Jan 26, 2018
I did a fork and corrected that but when I thank about it, it is more simple to just pass that handle than to explicit pass the number value so maybe the real correction would be to remove that int| from "@param {int|object} fn The callback function"
L2L2L
commented
Jan 26, 2018
|
I did a fork and corrected that but when I thank about it, it is more simple to just pass that handle than to explicit pass the number value so maybe the real correction would be to remove that int| from "@param {int|object} fn The callback function" |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
L2L2L
Jan 26, 2018
And I like for you to consider performance.now() would be a better decision than new Date().now();
here are two sources to help you consider this:
performance date now vs date gettime
https://stackoverflow.com/questions/12517359/performance-date-now-vs-date-gettime
performance now vs date now
https://stackoverflow.com/questions/30795525/performance-now-vs-date-now
It to me sound like what you have is correct but we are looking a performance and it seem from what I read, you can get better result with performance and timing with animation using the performance.now() so since we don't really need to know about the exact time and only for a precision of how long it been... So maybe like I said what you have already is better.
L2L2L
commented
Jan 26, 2018
|
And I like for you to consider performance.now() would be a better decision than new Date().now(); here are two sources to help you consider this: performance date now vs date gettime performance now vs date now It to me sound like what you have is correct but we are looking a performance and it seem from what I read, you can get better result with performance and timing with animation using the performance.now() so since we don't really need to know about the exact time and only for a precision of how long it been... So maybe like I said what you have already is better. |
are these shims also designed to work without Paul's window.requestAnimFrame? otherwise i see no point in duplicating the checks that paul already does in his. simply do
if (!window.requestAnimFrame) return window.setInterval(fn, delay);at the start.