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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:20:19+00:00 2026-05-26T07:20:19+00:00

I’m building a countdown clock in JavaScript and am having trouble formatting it exactly

  • 0

I’m building a countdown clock in JavaScript and am having trouble formatting it exactly how I want. Basically, I have a variable called totalTime which is initially set to the total number of seconds that the countdown will run for. Every second, this number decreases by 1, and I convert it into minutes and seconds for displaying on the page.

What’s tripping me up is that I want to include a leading 0 on the number of minutes remaining, but only if the initial value of totalTime is 600 (10:00) or greater. So, if I set totalTime to 600, I want the countdown to display 10:00, 09:59, 09:58, etc. (note the leading 0); but if I set totalTime to 300, I want the countdown to display 5:00, 4:59, 4:58, etc.

Put another way, the leading 0 should appear based on the amount of time the countdown starts off with (the initial value of totalTime), not how much time is currently left (the current value of totalTime). How would I do this?

EDIT: here’s the code I have currently: http://jsfiddle.net/JFYaq/

// Get the countdown element
var countdown = document.getElementById("countdown");

// Set the total time in seconds
var totalTime = 600;

// Every second...
var interval = setInterval(function(){
    // Update the time remaining
    updateTime();

    // If time runs out, stop the countdown
    if(totalTime == -1){
        clearInterval(interval);
        return;
    }
}, 1000);

// Display the countdown
function displayTime(){
    // Determine the number of minutes and seconds remaining
    var minutes = Math.floor(totalTime / 60);
    var seconds = totalTime % 60;

    // Add a leading 0 to the number of seconds remaining if it's less than 10
    if (seconds < 10){
        seconds = "0" + seconds;
    }

    // Split the number of minutes into two strings
    var minutesString = minutes + "";
    var minutesChunks = minutesString.split("");

    // Split the number of seconds into two strings
    var secondsString = seconds + "";
    var secondsChunks = secondsString.split("");

    // If the total time is 10 minutes or greater...
    if (totalTime >= 600){
        // Add a leading 0 to the number of minutes remaining if it's less than 10
        if (minutes < 10){
            minutes = "0" + minutes;
        }
        // Display the time remaining
        countdown.innerHTML = "<span>" + minutesChunks[0] + "</span>" + "<span>" + minutesChunks[1] + "</span>" + ":" + "<span>" + secondsChunks[0] + "</span>" + "<span>" + secondsChunks[1] + "</span>";
    }
    // If the total time is less than 10 minutes...
    else {
        // Display the time remaining without a leading 0 on the number of minutes
        countdown.innerHTML = "<span>" + minutes + "</span>" + ":" + "<span>" + secondsChunks[0] + "</span>" + "<span>" + secondsChunks[1] + "</span>"
    }        
}

// Update the time remaining
function updateTime(){
    displayTime();
    totalTime--;
}

// Start the countdown immediately on the initial pageload
updateTime();
  • 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-05-26T07:20:19+00:00Added an answer on May 26, 2026 at 7:20 am

    Fiddle: http://jsfiddle.net/JFYaq/1/

    Explanation:

    To prefix a zero when necessary, the following function can be used:

    function pad(n){
        return n > 9 ? "" + n : "0" + n;
    }
    

    Note "" + n. The pad(n) function will always return a string, which is useful for applying string methods.

    The padding should always be used at the second counter. For the minute counter, store the original time in a variable, and check whether it exceeds 600 or not:

    var original = 600;
    function padMinute(n){
        return original >= 600 && n < 9 ? "0" + n : "" + n;
    }
    

    Relating to your code:

    function displayTime(){
        var minutes = Math.floor(totalTime / 60);
        var seconds = totalTime % 60;
    
        minutes = "<span>" + padMinute(minutes).split("").join("</span><span>") + "</span>";
        seconds = "<span>" + pad(seconds).split("").join("</span><span>") + "</span>";
    
        countdown.innerHTML = minutes + ":" + seconds;
    }
    

    .split("") turns splits the string in a list of characters. .join("</span><span>") is used to concatenate the set of string, adding </span><span> between every character. The The whole result is joined with <span> and </span> so that the final HTML is valid.

    Execution model:

    1. padMinute(minutes)                       "09"
    2.             .split("")                   Array( "0" , "9" )
    3.                   .join("</span><span>")
                                                "0</span><span>9"
    4.                "<span>" + .. + "</span>" "<span>0</span><span>9<span>"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.