0

I want to use setInterval function for a repeated task. Here is my autoUpdate function which works one time.

var intervalId;

function autoUpdate() {
    intervalId = setInterval(updateFile(fileId, 'root', document.getElementById('editor').value), 10000);
}

Here is my stopAutoUpdate function.

function stopAutoUpdate() {
    clearInterval(intervalId);
}

What is wrong with this code? Thanx.

Edit= I have the title edited. Sorry.

Atti
  • 31
  • 1
  • 8

3 Answers3

3

You have to set the interval function correctly:

function autoUpdate() {
    intervalId = setInterval(function(){
        updateFile(fileId, 'root', document.getElementById('editor').value)
    }, 10000);
}

What you are doing is actually calling the function once instead of passing it. The clearInterval itself looks correct.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Lain
  • 3,657
  • 1
  • 20
  • 27
  • To clarify what @Lain is saying, you're calling the updateFile function instead of setting it as what should be run on each interval. edit: he cleared it up himself afterwards so ignore me :) – Pabs123 Dec 02 '16 at 20:26
  • @Lain Man isn't neutral. – Mike Cluck Dec 02 '16 at 20:29
  • @Mike C: Historically seen, it is/was and could be. A person used to be a man.. which a man can still see in expressions like 'mankind'. – Lain Dec 02 '16 at 20:30
  • @Lain im definitely a man, but i didnt wrestled a bear, at least until now. Thanx for the answer. – Atti Dec 02 '16 at 20:32
  • @Mike C: http://www.todayifoundout.com/index.php/2010/08/the-word-man-was-originally-gender-neutral/ – Lain Dec 02 '16 at 20:34
  • @Atti: A man is welcome :) – Lain Dec 02 '16 at 20:38
  • @Lain Sure, but I think you'd be hard pressed to get anyone to believe that the standard usage of it doesn't refer to a male. What it meant historically doesn't really matter now. – Mike Cluck Dec 02 '16 at 20:41
  • Mike C: Certainly. It is more of a fun thing from GoT. I did not think of it to be confusing/offending. – Lain Dec 02 '16 at 20:42
  • @Lain No worries. This line "[a] man can actually be neutral, whereas woman cant" made me think it was a misogyny thing instead of a GoT thing. – Mike Cluck Dec 02 '16 at 20:43
0

Problem is with setInterval param you set, it receives a function definition, but your giving the result of a function execution

leo.fcx
  • 6,137
  • 2
  • 21
  • 37
0

There's nothing wrong with clearInterval. You're using setInterval wrong and may not have the ID in scope. Here's a simplified example of how it should work.

// Make sure the ID is declared somewhere that both functions
// can access it
var intervalID;
var calls = 0;

function displayStuff(a, b, c) {
  console.log(a, b, c);
  calls++;
  
  if (calls >= 10) {
    stopAutoUpdate(); 
  }
}

function autoUpdate() {
  intervalID = setInterval(function() {
    // Notice how I'm wrapping the call in an anonymous function
    // without this, it will just call the function once, not repeat it
    displayStuff('Auto:', intervalID, calls);
  }, 800);
}

function stopAutoUpdate() {
  clearInterval(intervalID);
  console.log('Done');
}

// Start the interval
autoUpdate();
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91