前端節流經常在專案監聽頁面滾動的時候用到,就是在一段時間內,只執行一次
話不多說,看代碼
function throttle(func, deley) { let run = true,timer; return function () { if (!run) return; run = false; func.apply(this, arguments) timer = setTimeout(() => { run = true clearTimeout(timer); }, deley) } } document.body.addEventListener('mousemove',throttle(e=>{console.log(e)},3000))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/106472.html
標籤:JavaScript
上一篇:promise的常用情況
