在我的 JavaScript 中,当某些事件发生时,我会运行一些警报函数。我想清除用户移动鼠标时的所有警报。我可以使用下面的函数清除所有内容,但我只想在警报运行时设置该函数,然后清除 window.onmousemove (所以我不会在鼠标移动的所有内容上运行 if 语句)。有没有简单的解决办法?
window.onmousemove = function() {
if (alerts != null)
clearAlert.call();
};
最佳答案
使用.addEventListener()
和 .removeEventListener
function listener() {
if (alerts != null) {
clearAlert();
window.removeEventListener("mousemove", listener, false);
}
}
window.addEventListener("mousemove", listener, false);
关于javascript - 运行一次 window.onmousemove 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31924336/