JavaScript ES6 setInterval in a static class method

300 Views Asked by At

I want to use ES6 class definition in titanium-alloy mvc. I created a class named Clock that should work like a stopwatch similar to that one. If I try to translate this Clock class into ES6 then I'm not able to display the counter. The result string in the view/index.xml is NaN:NaN:NaN. I think it is about passing $.clockLbl. I read out this article about "Using ES6+ in a Titanium App" but was not able to apply. Could someone help please.

class Clock {
/*
    constructor(){
      this.totalSeconds = 0;
      this.isRunning = 0;
      this.intervall = null;
    }
  */  
    static start(clockLbl) {
        var self = this;
        this.isRunning = 1;
        this.interval = setInterval(function() {
            self.totalSeconds += 1;
            var hours = Math.floor(self.totalSeconds / 3600);
            var min = Math.floor(self.totalSeconds / 60 % 60);
            var sec = parseInt(self.totalSeconds % 60);
            clockLbl.text = '' + hours + ':' + min + ':' + sec;
        }, 1000);
    }
    
    static pause() {
        this.isRunning = 0;
        clearInterval(this.interval);
        delete this.interval;
    }
}
Clock.totalSeconds = 0;
Clock.isRunning = 0;
Clock.intervall = null;
module.exports = Clock;

My controller/index.js:

var Clock = require('stopwatch');
Clock.start($.clockLbl);
0

There are 0 best solutions below