0
會不會有什麼好處(或甚至可能在window.cancelAnimationFrame();
下面?取消請求動畫幀是否有優化?
這個例子如果是這樣,我怎麼會這麼做,爲什麼?
Ref: requestAnimationFrame MDN
var last_known_scroll_position = 0;
var ticking = false;
function doSomething(scroll_pos) {
// do something with the scroll position
}
window.addEventListener('scroll', function(e) {
last_known_scroll_position = window.scrollY;
if (!ticking) {
window.requestAnimationFrame(function() {
doSomething(last_known_scroll_position);
ticking = false;
});
}
ticking = true;
});
PS:對於一個更通用節流,我寫了一個[這裏](http://stackoverflow.com/a/38626349/ 3702797) – Kaiido