. 不做任何处理,鼠标移动时频繁触发
window.onmousemove = function (e) {
console.log(e.clientY);
}
. 借助定时器采用节流措施;一般延时100ms,为了直观,采用500ms
let timer = null;
window.onmousemove = function (e) {
if (timer) {
return;
}
timer = setTimeout(() => {
console.log(e.clientY);
timer = null;
}, 500)
}
. 封装节流函数
function Throttle(fn, delay = 100) {
let timer = null;
return function () {
if (timer) {
return;
}
timer = setTimeout(() => {
fn.apply(this, arguments);
timer = null;
}, 500)
}
}
window.onmousemove = Throttle((e) => {
console.log(e.clientY);
}, 500)