throttle
by Alex Liu
HTML
<button id="button">
click me
</button>
JavaScript
function throttle(func, timer) {
let before, now
return function() {
now = new Date()
if(!before || now - before >= timer){
func.apply(this, arguments)
before = new Date()
}
}
}
const button = document.getElementById("button")
function alert2(name){
console.log(`haha, ${name}`)
}
const alert2WithThrootle = throttle(alert2, 2000)
button.onclick = alert2WithThrootle