Reputation: 693
In my JavaScript I have a few alert functions running when certain events happen. I want to clear all the alerts when the user moves their mouse. I'm able to clear everything fine with the below function, but I only want to set the function when alerts are running and clear window.onmousemove afterwards (so I'm not running the if statement everything the mouse moves). Is there an easy fix for this?
window.onmousemove = function() {
if (alerts != null)
clearAlert.call();
};
Upvotes: 0
Views: 1070
Reputation: 21911
Use .addEventListener()
and .removeEventListener
function listener() {
if (alerts != null) {
clearAlert();
window.removeEventListener("mousemove", listener, false);
}
}
window.addEventListener("mousemove", listener, false);
Upvotes: 2