【发布时间】:2018-12-25 11:11:57
【问题描述】:
关于 JavaScript 的 requestAnimationFrame 已经有很多问题了,(我认为)我理解这个概念,但是在这种情况下使用和不使用 cancelAnimationFrame 之间是否存在性能差异?
// 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);
没有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);
我在每个代码上得到相同的结果。
但我想知道如果我不取消动画帧会发生什么。请求的函数是否堆积在内存中的某个地方或其他地方?
【问题讨论】:
标签: javascript requestanimationframe cancelanimationframe