Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8259981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:59:03+00:00 2026-06-08T02:59:03+00:00

I know a million JavaScript timer questions have been asked already, but I can’t

  • 0

I know a million JavaScript timer questions have been asked already, but I can’t figure this one out.

The #drive element is updated the first time (to 0) but not after that, as if either setTimeout() is not working or this.count++ is not working. Anyone know why this isn’t working?

Thanks…

var timer = {
    timerRunning: false,
    count: 0,
    delay: 1000,

    tick: function() {
        $("#drive").html(this.count);
        this.count++;
        setTimeout(function(){
            if (this.timerRunning)
                this.tick();
        }, this.delay);
    },

    start: function() {
        this.timerRunning = true;
        this.tick();
    },

    stop: function() {
        this.timerRunning = false;
    }
};

timer.start();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:59:05+00:00Added an answer on June 8, 2026 at 2:59 am

    The value of this inside your setTimeout() callback is no longer your timer object (it will be the window object). That’s why it isn’t working.

    You can fix it like this by setting a closure variable to your object and using that instead:

    tick: function() {
        $("#drive").html(this.count);
        this.count++;
        var self = this;
        setTimeout(function(){
            if (self.timerRunning)
                self.tick();
        }, this.delay);
    },
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there have been a million questions asking this, but mine is different.
I know this has been asked a million times before but i still can't
I know this has been asked a million times before, but I can't seem
I know that this question must have been asked and answered a million times,
I know this question has been asked a million times in one form or
I know that this has been asked a million times before, but nothing that
I know this has probably been asked a million times, but how do I
I know this has kinda been asked a million times but I wanted to
Hey all, I know this question has been asked a million times before but
I know this has been asked a million times, but I haven't had much

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.