I am using Javascript (no JQuery) and have a situation I'm trying to debug.
I'm using setInterval with a 1 second interval to monitor physical hardware that is being instructed to move. Once the hardware has moved to the correct position I call clearInterval with the ID returned from the setInterval call.
Everything works as anticipated - UNLESS... the hardware is not working properly.
When the hardware is not working properly, my first attempt to move the hardware returns an error (which is the way it should work).
When I receive an error, I want to stop the Interval by calling clearInterval.
However, what I'm seeing is this...
I call setInterval - which immediately fires off my first request to move the hardware which (in this case) returns an error (can not move hardware) very quickly.
It appears that the error is being returned faster the setInterval stores the Interval ID into the resulting variable. Thus, my call to clearInterval fails because the ID is zero, not the proper ID.
I've verified this... and indeed there is a significant delay on the setInterval call, to when it returns with the ID. So something like this is happening:
- I call setInterval and expect an ID back
- setInterval calls my function which tries to move the hardware
- Hardware move fails, my function tries to call clearInterval, but the original setInterval has not yet returned the proper ID
- clearInterval fails as the ID is zero, not the proper one
- my function continues to be called
I've tried to clearInterval all the values between 0 and 100 (as suggested in other posts) - but that does not seem to stop the timer (the ID I'm getting back is almost always 2, so I'm unclear why it won't clear it).
What would be the suggested method for ensuring I get the ID back in a timely fashion?
Note... not using (and can not due to requirement) JQuery. Just pure javascript.
Many thanks!
Edit: Adding code...
Globally declared:
var drawIntervalID = 0;
Here is my code that sets the Interval:
drawIntervalID = setInterval(masks_animate,1000);
The masks_animate function is a simple straight-forward function that simply makes some decisions and calls other functions. Specifically it is calling moveCarousel to actually move the hardware. The moveCarousel is also the function trying to clear the interval if something goes wrong:
// This function will move the carousel
function moveCarousel(mask) {
var res,i,ss;
setLO("","#000000");
if (training_mode) {
alert("Attempt to move Carousel in Training Mode ignored");
} else {
res = sendRequest("MOVE:"+mask);
i = res.indexOf('OK:');
if (i >= 0) {
ss = res.slice(i);
res = ss.split(":");
if (res.length == 2) {
res[1] = res[1].replace(/(\r\n|\n|\r)/gm,"");
setLO(res[1],"#00ff00");
} else {
alert("Carousel MOVE request returned bad data ("+ss+")");
clearTimer();
masksDisplay();
}
} else {
i = res.indexOf('ERR:');
if (i >= 0) {
ss = res.slice(i);
res = ss.split(":");
if (res.length == 2) {
res[1] = res[1].replace(/(\r\n|\n|\r)/gm,"");
setLO(res[1],"#ff0000");
} else alert("Carousel MOVE request returned bad data ("+ss+")");
} else alert("Carousel MOVE request returned bad data ("+res+")");
clearTimer();
masks_moving = 0;
masksDisplay();
}
}
return false;
}
The thing to look at is the lower case where I'm looking for "ERR:" (which is an error condition from the hardware. In that case I call clearTimer() which looks like this:
// This function will clear the timer
function clearTimer() {
if (drawIntervalID != 0) {
clearInterval(drawIntervalID);
drawIntervalID = 0;
} else {
for (var i=0; i<100; i++) clearInterval(i);
drawIntervalID = 0;
}
}
Like I said - it all works perfectly except in the situation where the hardware immediately returns an error, in which case my drawIntervalID is not set yet.
EDIT: I've modified the code to set the initial drawIntervalID to NULL (not zero) and am checking for NULL. No difference in operation.