代码实现
function throttle(func, time) {
let runFlag = true;
return function (e) {
if (runFlag) {
runFlag = false;
setTimeout(function () {
func(e);
runFlag = true;
}, time);
}
}
}
function debounce(func, time) {
let timer;
return function (e) {
clearTimeout(timer);
timer = setTimeout(function () {
func(e);
}, time);
}
}
window.addEventListener("mousemove", debounce(function (e) {
console.log(e);
}, 1000))
网友评论