mercredi 27 mai 2015

How to implement a stopwatch countdown using Ember.run.later

The solution boils down to "How to clear interval of Ember.run.later"

  class CountdownTimer {

  constructor() {
    this.counterContinue = false;
  }

  start() {
    this.counterContinue = true;
    this.decreaseCounterLoop();
  }

  stop() {
    this.counterContinue = false;
  }

  decreaseCounterLoop() {
    Ember.run.later(this, function() {
      //do work

      if (this.counterContinue) {
        this.decreaseCounterLoop();
      }
    }, 1000);
  }
}

I tried to implement a countdown timer where I can start and stop at will. But currently this has a bug.

I call stop(), and call start() again quick enough so the decreaseCounterLoop starts looping twice a second.




Aucun commentaire:

Enregistrer un commentaire