节流:不管如何点击都只会执行规定时间内的点击事件
content.onmousemove = throttle(count,1000);
// 定时器版
function throttle(func, wait) {
let timeout;
return function() {
let context = this;
let args = arguments;
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}
防抖:当你点击这个图按时不管你点击多少次 都只会执行第一次或最后一次的点击事件
content.onmousemove = count;
//短时间内多次触发同一事件,只执行最后一次,或者只执行最开始的一次,中间的不执行。
function debounce(func, wait) {
let timer;
return function() {
let context = this; // 注意 this 指向
let args = arguments; // arguments中存着e
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args)
}, wait)
}
}
content.onmousemove = debounce(count,1000);