Skip to content Skip to sidebar Skip to footer

Javascript Multiple Intervals And Clearinterval

I have a small program, when you click on an 'entry', the editing mode is opened, and the entry is to edit locked for others. There is every 10 seconds sends an ajax request to upd

Solution 1:

You're assigning multiple interval IDs to the same variable which will only hold the interval ID that was assigned to it last. When you clear the interval, only the interval corresponding to that ID will be cleared.

A straightforward solution would be to maintain an array of interval IDs, and then clear all intervals represented in the array. The code could look something like this:

var intervalIds = [];

$(".entry-edit").click(function() {
    intervalIds.push(setInterval(function() { loopLockingFunction(id) }, 10000));
});

$(".entry-cancel").click(function() {
    for (var i=0; i < intervalIds.length; i++) {
        clearInterval(intervalIds[i]);
    }
});

Solution 2:

maybe you can try like this.

var loopLockingVar;

$(".entry-edit").click(loopLockingVar,function() {
  // code

    loopLockingVar = setInterval(function() { loopLockingFunction(id) }, 10000);

  // code
});

Post a Comment for "Javascript Multiple Intervals And Clearinterval"