0

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);
Hölderlin
  • 424
  • 1
  • 3
  • 16
  • Decide whether you want a static module or create multiple instances. Your current code is a weird mix. – Bergi Dec 20 '20 at 22:49
  • thx, was not sure how to define static members. Because I saw how people access this.x in a static method and there is no explicite staitc keyword for members. Is it more common to use Clock.isRunning instead of this.isRunning in the method? – Hölderlin Dec 21 '20 at 03:57
  • Using `this.` in a `static` method is fine. If you want all static methods and properties, you wouldn't [(shouldn't) use a `class` at all](https://stackoverflow.com/a/29895235/1048572). – Bergi Dec 21 '20 at 09:16

0 Answers0