HTML:
<button id="btn" onclick="btn()">
Click!
</button>
JS:
function throttle(func, wait) {
let lastTime = null
let timeout
return function() {
let context = this
let now = new Date()
if (now - lastTime - wait > 0) {
if (timeout) {
clearTimeout(timeout, '111')
timeout = null
}
func.apply(context, arguments)
lastTime = now
} else if (!timeout) {
timeout = setTimeout(() => {
func.apply(context, arguments)
}, wait)
}
}
}
let throttleRun = throttle(() => {
console.log(123)
}, 5000)
function btn() {
throttleRun()
}