Rejoanul Alam
Rejoanul Alam

Reputation: 5398

Provide setInterval's ID and clear by ID

How can I provide specific ID to setInterval() and then call clearInterval() by ID.

$('.notify').each(function () {
    var chkr = $(this).find('span').text();

    if (chkr == 'Offline') {
        if (typeof(clearC) !== 'undefined') {
            clearInterval(clearC);
        }

        // how can i provide the ID of "clearC" below
        clearC = setInterval(function () {
            soundOn();
        }, 500);
    }
    else {
        //how can i clear "clearC" with the ID provided in if statement
        if (typeof(clearC) !== 'undefined') {
            clearInterval(clearC);
        }
    }
});

The Problem is when if statement execute then sound goes on. But for next row its enter in else statement (because "Online") and clear the interval although it should not be.

So my concern is to clear clearInterval for specific row until there "Online" text found.

Upvotes: 4

Views: 835

Answers (4)

sergdenisov
sergdenisov

Reputation: 8572

What about this way?

$('.notify').each(function() {
    var $this = $(this);
    var intervalID = $this.data('intervalID');
    var chkr = $this.find('span').text();

    clearInterval(intervalID);
    if (chkr === 'Offline') {
        $this.data('intervalID', setInterval(function() { soundOn(); }, 500));
    }
});

P.S. You don't need to check variable with interval ID on undefined, there is no real profit in performance, but unnecessary code.

Upvotes: 1

Nitesh Patel
Nitesh Patel

Reputation: 631

It seems like you want to make sure you keep track of the intervalIds for each of the objects. So store the intervalId on the objects themselves, using the jQuery .data feature:

clearC = setInterval(function () { soundOn(); }, 500);
$(this).data("intervalId", clearC);

When you want to retrieve it:

clearInterval($(this).data("intervalId"));

Upvotes: 1

You can declare clearC as an object {}, create and store its ID in the .id property, and setInterval() in its .interval property. In this way you can check its ID by getting clearC.id.

var clearC = {};
// ...
$('.notify').each(function(){
  var chkr = $(this).find('span').text();

  if(chkr == 'Offline'){
    if(typeof(clearC.interval) !== 'undefined'){
      clearInterval(clearC.interval);
    }

    // this way you can provide the ID of "clearC.interval" below
    clearC.id = "your-id";
    clearC.interval = setInterval(function () {
      soundOn();
    }, 500);
  }
  else{
    // this way you can clear "clearC.interval" with the ID provided in if statement
    if(typeof(clearC.interval) !== 'undefined'){
      if (clearC.id === "your-id")
        clearInterval(clearC.interval);
    }
  }
});

Upvotes: 1

mooiamaduck
mooiamaduck

Reputation: 2156

Your code is already correct, so I'm a little confused.

It's pretty simple. The id is the return value of setInterval. setInterval does not accept an id to an existing timer -- what do you expect it would be used for?

https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval

Upvotes: 0

Related Questions