// 防抖
function debounce(fn, wait){
let timer = null;
return function(){
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, arguments)
}, wait)
}
}
// 节流
function throttle(fn, wait){
let time = 0;
return function(){
let now = Date.now();
let arg = arguments;
if(now - time >= wait){
fn.apply(this, arg)
time = now
}
}
}
function sayHeelo(){
console.log('Hello,world!')
}
document.addEventListener('mousemove', throttle(sayHeelo, 2000))
const btn = document.getElementsByClassName('btn')[0];
btn.addEventListener('click', dbounce(sayHeelo, 1000))
本文链接:https://blog.smallhao.fun/?id=22 转载需授权!
暂无评论
发表评论