用javascript写一个简版节流函数
function throttle(fn, interval) { let lastTime = 0; return function(...args) { const now = Date.now(); if (now - lastTime >= interval) { fn.apply(this, args); lastTime = now; } } }
// 示例 function onScroll() { console.log('scrolling'); }
const throttledScroll = throttle(onScroll, 100); window.addEventListener('scroll', throttledScroll);
原文地址: https://www.cveoy.top/t/topic/tJg 著作权归作者所有。请勿转载和采集!