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 8563619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:59:59+00:00 2026-06-11T16:59:59+00:00

I am working on a countdown timer written in Javascript. Fairly basic really. Just

  • 0

I am working on a countdown timer written in Javascript. Fairly basic really. Just uses setInterval for the timing aspect. I wrote it using the prototype method of storing functions and variables so I can create a “class”.

I call the code in this fashion.

function testTimer() {
    var newTimer = new CDTimer($("#voteTimer"),30,"");
    newTimer.start();
}

When the below code runs, console.log is printing out undefined or NaN.

function CDTimer (target, duration, callback) {
    this.target = target;
    this.duration = duration;
    this.callback = callback;
}

CDTimer.prototype.start = function() {
    this.start = new Date().getTime();
    this.interval = setInterval(this.update, 1000);
}

CDTimer.prototype.update = function() {
    console.log(this.duration, this.start);
    this.elapsed = this.duration - (new Date().getTime() - this.start) / 1000

    if (this.elapsed < 0) {
            clearInterval(this.interval);
            this.callback();
    }
    else {
        console.log(this.elapsed);
        $(this.target).text(this.elapsed);
    }
}

CDTimer.prototype.stop = function() {
    clearInterval(this.interval);
}

I must be missing something silly. What is happening to my variables and their values?

Thanks for the insight.

  • 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-11T17:00:00+00:00Added an answer on June 11, 2026 at 5:00 pm

    The function called from setInterval is provided a this which is the window, not the timer.

    You may do this :

    CDTimer.prototype.start = function() {
        this.start = new Date().getTime();
        var _this = this;
        this.interval = setInterval(function(){_this.update()}, 1000);
    }
    

    Note that the MDN offers a detailed explanation.

    EDIT following comment : if you don’t want to create a new variable in the start function, you could do this :

    CDTimer.prototype.start = function() {
        this.start = new Date().getTime();
        this.interval = setInterval(function(_this){_this.update()}, 1000, this);
    }
    

    But I’m not sure the readibility is improved by this move of the variable creation and it’s not compatible with IE (if you don’t patch it, see MDN’s solution).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a this working countdown timer in jQuery but for now I'm just
Trying to make a small countdown timer in my app but it's not working.
Working on a small game using an HTML5 canvas, and javascript. And it's working
I am using the jquery countdown timer here http://www.littlewebthings.com/projects/countdown/ and I have found that
I'm working on a auto logout using setTimeout and clearTimeout. Should be really straight
I'm trying to make a countdown timer similar to the one in rapidshare using
I have made separate project for countdown timer and it is working fine. Now
I'm working on an iOS app that uses an NSTimer for a countdown. This
I am working with a countdown timer and have it set so that it
I'm working on my first jQuery plugin which is a simple countdown timer with

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.