1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
const debounce = function (fn, wait = 1000) { let timer = null return function () { if (timer) clearTimeout(timer) const _this = this const _args = [...arguments] timer = setTimeout(() => { fn.apply(_this, _args) }, wait); } }
const throttle = function (fn, wait) { let timer = null return function (...args) { if (timer) return timer = setTimeout(() => { fn.apply(this, args) clearTimeout(timer) timer = null }, wait); } }
|