触发的事件以周期的形式去执行,而非实时。如滴水的水龙头。
function throttle (fn, delay) { // 利用闭包变量时效性 let timeout let arg return function () { arg = arguments if (!timeout) { timeout = settimeout(() => { fn.apply(this, arg) timeout = null }, delay) } } } // demo /* var test = throttle(function (a) {console.log(a)}, 1000) test(1) // 不执行 test(2) // 不执行 test(3) => 3 test = null // 不需要时释放内存 */
事件最后一次触发的n毫秒后触发,如电梯门。
function debounce (fn, delay){ let timeout return function(){ const args = arguments cleartimeout(timeout) timeout = settimeout(() => { fn.apply(this, args) }, delay) } } // 用法同throttle
如对本文有疑问, 点击进行留言回复!!
荐 css的3中水平居中方式和4中水平垂直居中方式及应用情形速记
网友评论