function throttle(fn, delay) { let last = 0 // 上次触发时间 return function (...args) { const now = Date.now() if (now - last > delay) { last = now fn.apply(this, args) } } } // 测试 function task() { console.log('run task') } const throttleTask = throttle(task, 1000) window.addEventListener('scroll', throttleTask)
标签:function,task,last,节流,throttleTask,delay,now From: https://www.cnblogs.com/qingshuihongye/p/16721466.html