var timer = null;
document.getElementById("wholeDocument").onmousemove= function(evt) {
if (timer) {
clearInterval(timer);
timer = null;
}
x = evt.clientX;
y = evt.clientY;
var timer = setInterval(function() { fnTimer(x, y); }, 50);
};
function fnTimer(x, y) {
// your code here
}
setInterval + clearIntervalInside + parameters -no jquery-
-
09-12-2021 - |
Question
I need to call a function periodically with setInterval
and passing parameters to it. At the same time, I need to clear the interval inside the function that's being called when the mouse is moved.
So I'm trying this:
var timer = setInterval(function(x,y){ // When I use this, x and y are undefined.
/*
Code
*/
document.getElementById("wholeDocument").onmousemove=clearInterval(timer);
}, 50);
The idea is to know how to use setinterval with a clearInterval inside and being able to pass parameters.
I would appreciate any help.
Solution
OTHER TIPS
Apart from the concept which could possibly be improved depending on what you're trying to achieve, this line
document.getElementById("wholeDocument").onmousemove=clearInterval(timer);
should be
document.getElementById("wholeDocument").onmousemove = function(e) { clearInterval(timer); }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow