0

I'm doing a web application:Pomodoro.

I created a class. But I can't access the function in this this.countdown = setInterval(timer,1000).

I writed "this.timer". This way, the error does not returning, but the function does not working. How should I do ?

class Pomodoro{
    constructor(){
        this.countdown = 0;
        this.seconds = 1500;
        this.workTime = 25;
        this.breakTime = 5;
        this.isBreak = true;
        this.isPaused = true; 
        this.minutes = Math.floor(this.seconds / 60);
        this.remainderSeconds = this.seconds % 60;
    };
    start(){
        clearInterval(this.countdown);
        this.isPaused = !this.isPaused;
        if(!this.isPaused){
            this.countdown = setInterval(timer,1000);
        };
    };
    reset(){
        clearInterval(this.countdown);
        this.seconds = this.workTime*60;
        this.countdown = 0;
        this.isBreak = true;
        this.isPaused = true; 
    };
    timer(){
        this.seconds --;
        if(this.seconds < 0){
            clearInterval(this.countdown);
            this.seconds = (this.isBreak ? this.breakTime : this.workTime) * 60;
        }
    };
}

let pomodoro = new Pomodoro();
function Update(){
    pomodoro.start();
    pomodoro.timer();
}
window.setInterval(Update, 100);
  • 1
    In your `start` function. You've referenced a variable `timer` but where is this coming from? It's currently undefined. `this.countdown = setInterval(timer,1000);` – Karl Taylor May 24 '19 at 12:33
  • 3
    `this.countdown = setInterval(this.timer.bind(this),1000);` – FrV May 24 '19 at 12:35
  • 2
    There is no `timer` variable in scope where you're trying to use it ([more](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript)). You'd want `this.timer` ([more](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work)). But you also need to ensure `this` is correct within `timer` when it gets called, [details here](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback). – T.J. Crowder May 24 '19 at 12:36

0 Answers0