mardi 4 août 2015

Emberjs Countdown - not stoppable

Heyho

I have a little issue with my countdown written in Ember. More precisely in stopping my counter when it hits 0.

First of all... I'm using

Ember Version

DEBUG: Ember                    : 1.12.0

I've created a 'service' Class with some simple methods to handle the countdown process.

interval: function() {
  return 10; // Time between polls (in ms)
}.property().readOnly(),

totalTime: function() {
  return 5000; // Total Time (in ms)
}.property(),

timeDiff: 0,
timeLeft: function() {
  return Math.floor((this.get('totalTime') - this.get('timeDiff')) / 1000);
}.property('timeDiff'),

hasFinished: function() {
  return this.get('timeLeft') === 0;
}.property('timeLeft'),


// Schedules the function `f` to be executed every `interval` time.
schedule: function(f) {
  return Ember.run.later(this, function() {
    f.apply(this);
    this.set('timer', this.schedule(f));
  }, this.get('interval'));
},


// Starts the countdown, i.e. executes the `onTick` function every interval.
start: function() {
  this.set('startedAt', new Date());
  this.set('timer', this.schedule(this.get('onTick')));
},


// Stops the countdown
stop: function() {
  Ember.run.cancel(this.get('timer'));
},


onTick: function() {
  let self = this;
  self.set('timeDiff', new Date() - self.get('startedAt'));
  if (self.get('hasFinished')) {
    // TODO: Broken - This should stop the countdown :/
    self.stop();
  }
}

CountDown with Ember.run.later()

I'm starting the countdown within my controller (play action). The countdown counts down as it should but it just doesn't stop :(

The self.stop() call in onTick() just doesn't do anything at all...

I tried to stop the countdown with an other action in my controller and that is working as it should :/

Any ideas how to solve that problem??

Cheers Michael




Aucun commentaire:

Enregistrer un commentaire