防抖函数debounce(定时器)
一般用于输入框中,防止重复输入,重复发送请求
function debouce(func, delay){
let timer = null
return function(...args){
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
节流函数throttle(定时器)
一般用于页面下拉和上拉
function throttle(func, delay) {
let timeout = null;
return function(...args) {
if (!timeout) {
timeout = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
}
简易使用
function a(){
alert('ok')
}
var is_click = true
function tt() {
if(is_click){
a()
setTimeout(function () {
is_click = true
console.log(is_click)
}, 2000)
}
}