I am currently storing a bunch of setInterval ID's in an array.
How I initially set the intervals:
intervalId = setInterval(bridgeCall, 10000);
interValArray.push(intervalId);
I currently have a button that enables me to stop all intervals currently running by calling this function:
function stopCampaign() {
if (interValArray.length > 0) {
for (i = 0; i < interValArray.length; i++) {
clearInterval(interValArray[i]);
console.log("Stopped");
}
error = "Stopped"
Error();
} else {
error = "Nothing Running"
Error();
}
}
And this works accordingly, So I have tried to do the opposite to start them all back up again as seen below:
function Campaign() {
if (interValArray.length > 0) {
for (i = 0; i < interValArray.length; i++) {
setInterval(interValArray[i],10000);
console.log("Start");
}
error = "Started"
Error();
} else {
error = "No Agents"
Error();
}
}
But keep getting an error saying:
timers.js:275
timer._repeat();
^
TypeError: timer._repeat is not a function
at wrapper [as _onTimeout] (timers.js:275:11)
at Timer.listOnTimeout (timers.js:92:15)
Is it possible to setInterval/Start them up again?
The function
setInterval(function, interval)
returns an ID of the interval, which you already understand as you're storing them in an array.However, you can't just 'restart' an interval using only its ID. You'd have to clear the interval first, then call
setInterval()
using the initial parameters used when you stored the items in the array.You could modify the items in the array to keep all the information necessary to restart the timeouts:
Your new
stopCampaign()
would look something like this:And your new
Campaign()
would look something like this:You can't use the timer handle to do anything other than cancel the timer. This line:
tries to "restart" the interval using the handle. That simply isn't supported by the built-in timer stuff. You need to do the same thing you did to start it originally to start it again.
You haven't shown how you start them in the first place, so I can't suggest how you would restructure to make it possible to do it again, but here's a synthetic example: