<button onclick="debounceFn()">防抖点击</button>
const debounceFn = debounce(print, 2000)
function debounce (callback, delay = 3000) {
let timer = null
return function () {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
callback()
}, delay);
}
}
function print () {
console.log(123);
}
const throttleFn = throttle(print, 2000)
function throttle (callback, delay = 2000) {
let timer = null
return function () {
if (timer) return
callback()
timer = setTimeout(() => {
timer = null
}, delay);
}
}
function print () {
console.log(123);
}