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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:16:08+00:00 2026-06-16T18:16:08+00:00

I would like to create a simple timer in Javascript that counts down from

  • 0

I would like to create a simple timer in Javascript that counts down from a given time until it hits 0. I found this tutorial which worked perfectly. My problem is that I need to place multiple timers on the same page. This tutorial obviously won’t do that because it uses global variables (I’m new to JS/Programming so I might not be using the right terms). I tried to re-create the same thing only creating each timer as it’s own Object so that they don’t interfere with eachother. This is what I have.

function taskTimer(name, startTime) {
  this.timer = name;
  this.totalSeconds = startTime;
  this.tick = function() {
    if (this.totalSeconds <= 0) {
      return;
    }
    this.totalSeconds -= 1;
    this.updateTimer();
    // window.setTimeout("this.tick()", 1000);
  };
  this.updateTimer = function(){
    this.seconds = this.totalSeconds;

    this.hours = Math.floor(this.seconds / 3600);
    this.seconds -= this.hours * (3600);

    this.minutes = Math.floor(this.seconds / 60);
    this.seconds -= this.minutes * (60);

    this.timeString = this.leadingZero(this.hours) + ":" + this.leadingZero(this.minutes) + ":" + this.leadingZero(this.seconds);
    return this.timeString;
  };
  this.leadingZero = function(time){
    return (time < 10) ? "0" + time : + time;
  };
}
var testTimer = new taskTimer("timer", 30);
testTimer.tick();

I created one at the end there. Running
testTimer.updateTimer(); returns 00:00:30 which is correct, but running testTimer.tick(); returns no value. There is obviously something wrong with that part of the code I just can’t figure it out.

  • 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-16T18:16:09+00:00Added an answer on June 16, 2026 at 6:16 pm

    You’ve got a few problems.

    1. You’re calling updateTimer() inside of your tick method, so it
      won’t ever reach outside of there unless you return it.
    2. With your current setup, you’d have to call tick manually every time you wanted to update the clock, and if you don’t do that precisely every one second the timer will be inaccurate.
    3. To go with #2, you shouldn’t decrement totalSeconds like you are because it isn’t guaranteed that it will be exactly one second between triggers of your timeout. Use dates instead.

    Here’s what I would do: http://jsfiddle.net/R4hnE/3/

    // I added optional callbacks. This could be setup better, but the details of that are negligible. 
    function TaskTimer(name, durationInSeconds, onEnd, onTick) {
        var endTime,
            self = this, // store a reference to this since the context of window.setTimeout is always window
            running = false;
        this.name = name;
        this.totalSeconds = durationInSeconds;
    
        var go = (function tick() {
            var now = new Date().getTime();
            if (now >= endTime) {
                if (typeof onEnd === "function") onEnd.call(self);
                return;
            }
            self.totalSeconds = Math.round((endTime - now) / 1000); // update totalSeconds placeholder
            if (typeof onTick === "function") onTick.call(self);
            window.setTimeout(tick, 1000 / 12); // you can increase the denominator for greater accuracy. 
        });
    
        // this is an instance method to start the timer
        this.start = function() {
            if (running) return; // prevent multiple calls to start
    
            running = true;
            endTime = new Date().getTime() + durationInSeconds * 1000; // this is when the timer should be done (with current functionality. If you want the ability to pause the timer, the logic would need to be updated)
            go();
        };
    }
    // no reason to make this an instance method :)
    TaskTimer.prototype.toTimeString = function() {
        var hrs = Math.floor(this.totalSeconds / 60 / 60),
            min = Math.floor(this.totalSeconds / 60 - hrs * 60),
            sec = this.totalSeconds % 60;
    
        return [hrs.padLeft("0", 2), min.padLeft("0", 2), sec.padLeft("0", 2)].join(" : ");
    };
    
    var task = new TaskTimer("task1", 30, function() {
        document.body.innerHTML = this.toTimeString();
        alert('done');
    }, function() {
        document.body.innerHTML = this.toTimeString();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a simple control that inherits from HeaderedContentControl, and has
I would like to create a simple XMPP client in java that shares his
I'm developing a simple web quiz and using javascript, I would like to create
I would like to create simple objects at runtime (textbox, label, etc) and add
I would like to create a simple fluid layout whereby 4 columns of Equal
I would like to create a simple designer which looks like visual studio. Specifically,
I would like to create an online, simple WYSIWYG drawing editor allowing people to
I have a simple factory below that I would like to simplify and not
I would like to be able to convert simple one digit time strings into
Using the Northwind sample: I would like to create an insert trigger on the

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.