There are so many questions about JavaScript's requestAnimationFrame
already and (I think) I understand the concept but is there any performance difference between with and without cancelAnimationFrame
in this context?
// Setup a timer
var timeout;
// Listen for resize events
window.addEventListener('scroll', function () {
console.log( 'no debounce' );
// Cancel last animation, if there's one
if (timeout) {
window.cancelAnimationFrame(timeout);
}
// Setup the new requestAnimationFrame()
timeout = window.requestAnimationFrame(function () {
// Run our scroll functions
console.log( 'debounced' );
});
}, false);
without cancelAnimationFrame
:
// Setup a timer
var timeout;
// Listen for resize events
window.addEventListener('scroll', function () {
console.log( 'no debounce' );
// Setup the new requestAnimationFrame()
window.requestAnimationFrame(function () {
// Run our scroll functions
console.log( 'debounced' );
});
}, false);
I get the same result on each code.
But I want to know what happens if I don't cancel the animation frame. Does requested function get stacked somewhere in memory or something?