Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
I can't figure out why my timer object doesn't work

I tried to code a JavaScript timer that would increment a value each second and update the innerHTML of a DOM element to show the time. The first tick works, but after that timerEngine.time goes undefined and incrementing it in tick() produces NaN. Why does the variable go undefined though?

Here's the code:

const timerEngine = {
    time: 0,
    timerID: -1,

    tick() {
        this.time++;
        if (this.time % 60 < 10)
            document.getElementById("timer").innerHTML = `${Math.floor(this.time / 60)}:0${this.time % 60}`;
        else
            document.getElementById("timer").innerHTML = `${Math.floor(this.time / 60)}:${this.time % 60}`;
    },

    start() {
        if (this.timerID === -1)
            this.timerID = setInterval(this.tick, 1000);
    },

    stop() {
        if (this.timerID !== -1) {
            clearInterval(this.timerID);
            this.timerID = -1;
        }
    },

    reset() {
        this.stop();
        this.time = -1;
        this.tick();
    }
}

The timer is started in a function:

someFunction() {
    // some code
    timerEngine.reset();
    timerEngine.start();
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!