函数防抖
var inputs = document.getElementById("throttle")
var values = inputs.value //获取文本框内的值
var timerId = null // null = false
button.onclick = function() {
//如果 timerId 有值就打断上一次的 setTimeout 执行
if(timerId){
widow.clearTimeout(timerId)
}
//重新执行
timerId = setTimeout(()=>{
timerId = null
console.log(values) // 打印值
}, 5000) //5秒后再执行
}
函数节流
function fn() {
console.log('3秒执行一次')
}
var cd = false
button.onclick = function() {
if(cd){
return
}else{
fn() // 执行方法
cd = true
var timerID = setTimeout(()=>{
cd = false //一次性定时器重新赋值给cd
},3000)
}
}