function throttle(fn, delay) {
let timer = null
let lastTime = 0
const throttled = function (...args) {
const now = Date.now()
if (now - lastTime >= delay) {
lastTime = now
fn.apply(this, args)
}
clearTimeout(timer)
timer = setTimeout(() => {
lastTime = 0
}, delay)
}
throttled.cancel = function () {
clearTimeout(timer)
lastTime = 0
timer = null
}
return throttled
}