Skip to content Skip to sidebar Skip to footer

Setinterval Fires Only Once

I'm trying to create a self contained timer where all variables are inside a object. For the most part it works but I can get this to fire once. What am I missing? function Cou

Solution 1:

I modified your code a bit and changed the line containing setInterval to this:

this.timerId = setInterval((function(scope) {returnfunction() {scope.decrementCounter(scope);};})(this), 1000);

The functions run inside of setInterval run in the window scope. It only runs once, because you don't pass the function itself just the result of it. You need to return the actual function or pass an anonymous function which calls it.

jsfiddle demo: http://jsfiddle.net/2gLdL/1/

Solution 2:

You are calling the function, not assigning a reference

this.timerId = setInterval(this.decrementCounter(this), 1000); 

Seems weird you are passing in "this" as an argument...

use bind()

this.timerId = setInterval(this.decrementCounter.bind(this, this), 1000); 

or a closure

var that = this;
this.timerId = setInterval(function(){that.decrementCounter(that); }, 1000); 

Solution 3:

In the following bit of code:

this.timerId = setInterval(this.decrementCounter(this), 1000);

You are executing this immediately:

this.decrementCounter(this)

And so the return value of that, is what is being called by setInterval each second. Usually you would want to pass a function closure, like this:

var _this = this; //make sure your variable is available in the scope that followsthis.timerId = setInterval(function() { this.decrementCounter(_this); }, 1000);

Then, when your timer is executed, it's the function which is called, and that function then executes what you want. The other option is to pass the first parameter as a string such as "callAFunction('blah')" which will be evaluated each second and executed, but I believe the above is that you want.

Solution 4:

Try this:

setInterval(function(){Countdown()},500);

Solution 5:

I provided an answer to another question that creates a simple stopwatch.

You could modify the code slightly to count down instead of up.

Here's a jsbin demo.


Original snippet

varStopwatch = function(elem, options) {

  var timer       = createTimer(),
      startButton = createButton("start", start),
      stopButton  = createButton("stop", stop),
      resetButton = createButton("reset", reset),
      offset,
      clock,
      interval;

  // default options
  options = options || {};
  options.delay = options.delay || 1;

  // append elements     
  elem.appendChild(timer);
  elem.appendChild(startButton);
  elem.appendChild(stopButton);
  elem.appendChild(resetButton);

  // initializereset();

  // private functionsfunctioncreateTimer() {
    returndocument.createElement("span");
  }

  functioncreateButton(action, handler) {
    var a = document.createElement("a");
    a.href = "#" + action;
    a.innerHTML = action;
    a.addEventListener("click", function(event) {
      handler();
      event.preventDefault();
    });
    return a;
  }

  functionstart() {
    if (!interval) {
      offset   = Date.now();
      interval = setInterval(update, options.delay);
    }
  }

  functionstop() {
    if (interval) {
      clearInterval(interval);
      interval = null;
    }
  }

  functionreset() {
    clock = 0;
    render();
  }

  functionupdate() {
    clock += delta();
    render();
  }

  functionrender() {
    timer.innerHTML = clock/1000; 
  }

  functiondelta() {
    var now = Date.now(),
        d   = now - offset;

    offset = now;
    return d;
  }

  // public APIthis.start  = start;
  this.stop   = stop;
  this.reset  = reset;
};

Post a Comment for "Setinterval Fires Only Once"