Studento919 February 2016

Clear all running intervals

I am trying to clear all intervals at the same time with the same ID.

Reason for this is so multiple agents can have calls originated to them, so it works accordingly but when more than one agent/interval is involved the interval is not cleared and to the best of my understanding clearInterval only ever clears the last interval.

Is it possible to clear all Intervals all the same time with the same ID? or just clear all intervals?

I looked at the other questions regarding the same topic but didn't work for me and wondered if there was an up to date method of handling this.

Basic Code Idea:

var extenArray = [];

//Passport.js
app.get('/index', checkLoggedIn, function (req, res) {
    res.render('index', {
      isAuthenticated : req.isAuthenticated(),
      user : req.user
    });
    if(extenArray.length > 0){ 
      intervalId = setInterval(bridgeCall, 10000);
      console.log("Running");
    }
  });

  function bridgeCall() {
    if (extenArray.length > 0) {
       console.log("Begin!")
       console.log("value removed from array");
    } else {
      error = ("No extensions left to dial");
      Error();
      if(extenArray.length < 1){
      clearInterval(intervalId);
      console.log("Cleared Interval")
      }
    }
  }

Every time a extension is called its removed from the array but I want to keep the code sample as possible, but can flesh it out if required.

Is there a way to clear all the intervals with the same ID at the same time?

Answers


Studento919 February 2016

Thanks to @JaromandaX for the suggestion I configured my code and it works based of the suggestion to keep all of all interval ID's in an array.

Made modifications to the code sample given:

var extenArray = [];
var interVal = [];

//Passport.js
app.get('/index', checkLoggedIn, function (req, res) {
    res.render('index', {
        isAuthenticated : req.isAuthenticated(),
        user : req.user
    });
    if (extenArray.length > 0) {
        intervalId = setInterval(bridgeCall, 10000);
        interValArray.push(intervalId);
        console.log("Running");
    }
});

function bridgeCall() {
    if (extenArray.length > 0) {
        console.log("Begin!")
        console.log("value removed from array");
    } else {
        error = ("No extensions left to dial");
        Error();
        if (extenArray.length < 1) {
            for (i = 0; i < interValArray.length; i++) {
                clearInterval(interValArray[i]);
                console.log("Cleared Interval")
            }
        }
    }
}

Post Status

Asked in February 2016
Viewed 3,264 times
Voted 13
Answered 1 times

Search




Leave an answer