9

Here is a fiddle.

I'm trying to create a countdown object that uses moment.js (a plugin that I prefer over using Date())

var Countdown = function(endDate) {
    this.endMoment = moment(endDate);

    this.updateCountdown = function() {
        var currentMoment, thisDiff;

        currentMoment = moment();
        thisDiff = (this.endMoment).diff(currentMoment, "seconds");

        if (thisDiff > 0)
            console.log(thisDiff);
        else {
            clearInterval(this.interval);
            console.log("over");
        }
    }

    this.interval = setInterval(this.updateCountdown(), 1000);
}

I then create a instance of the countdown like so:

var countdown = new Countdown("January 1, 2014 00:00:00");

However the function only seems to run one time. Any ideas? Should I be using setTimeout() instead?

dougmacklin
  • 2,560
  • 10
  • 42
  • 69

2 Answers2

16

You should pass a reference to function, not the result of its execution. Also, you need some additional "magic" to call a method this way.

var me = this;
this.interval = setInterval(function () {
    me.updateCountdown();
}, 1000);
kirilloid
  • 14,011
  • 6
  • 38
  • 52
4

You can either store your this context as a local variable like the following:

var Countdown = function(endDate) {
  var self = this;
  this.endMoment = moment(endDate);

  this.updateCountdown = function() {
      var currentMoment, thisDiff;

      currentMoment = moment();
      thisDiff = (self.endMoment).diff(currentMoment, "seconds");

      if (thisDiff > 0)
          console.log(thisDiff);
      else {
          clearInterval(self.interval);
          console.log("over");
      }
  }

  this.interval = setInterval(this.updateCountdown, 1000);
}

Or you can just use your variables directly such as:

var Countdown = function(endDate) {
  var endMoment = moment(endDate);

  this.updateCountdown = function() {
      var currentMoment, thisDiff;

      currentMoment = moment();
      thisDiff = (endMoment).diff(currentMoment, "seconds");

      if (thisDiff > 0)
          console.log(thisDiff);
      else {
          clearInterval(interval);
          console.log("over");
      }
  }

  var interval = setInterval(this.updateCountdown, 1000);
}

I prefer the second approach - fiddle

fbynite
  • 2,651
  • 1
  • 21
  • 25