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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:19:30+00:00 2026-06-16T05:19:30+00:00

Probably not the clearest title, but here goes – I need to display two

  • 0

Probably not the clearest title, but here goes – I need to display two independent countdowns on a page, accepting a user input for the starting value for each. When one reaches zero, the other starts and counts down to zero. Code for this is below, and working as expected.

I call the Timer1 function, which checks a variable for the starting value, if it exists, the count starts. When the count is zero, I clear the interval, reset the display to the starting value, and fire the second timer, if it has a value assigned:

    function Timer1() {
        var gDuration = goTime;
        countdown = setInterval(function () {
            if (gDuration >= 0) {
                $("#durationValue").html(ToTime(gDuration));
                gDuration--;
            }
            else {
                clearInterval(countdown);
                $("#durationValue").html(ToTime(goTime));
                if (restTime != 0) {
                    Timer2();
                }
            }
        }, 1000);
    }

    function Timer2() {
        var rDuration = restTime;
        countdown = setInterval(function () {
            if (rDuration >= 0) {
                $("#restValue").html(ToTime(rDuration));
                rDuration--;
            }
            else {
                clearInterval(countdown);
                $("#restValue").html(ToTime(restTime));
            }
        }, 1000);
    }

The next step is to allow that process to run for a set number of loops – I’ve tried wrapping setInterval in Timer1 in a for loop, which doesn’t work. Any ideas how to better go about this?

  • 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-16T05:19:31+00:00Added an answer on June 16, 2026 at 5:19 am

    for-loops don’t work well with asynchronous stuff. Just make it a counter with an end condition as you have demonstrated with g/rDuration already.

    With some callback abstractions, and heavy continuation-passing-style:

    function timer(el, duration, interval, callback) {
        var countdown = setInterval(function() {
            if (duration-- >= 0) {
                el.text(ToTime(duration));
            } else {
                clearInterval(countdown);
                callback();
            }
        }, interval);
    }
    
    var goTime = …, restTime = …;
    function t1(cb) {
        timer($("#durationValue"), goTime, 1000, cb);
    }
    function t2(cb) {
        timer($("#restValue"), restTimer, 1000, cb);
    }
    var loops = …;
    (function loop(cb) {
        t1(function(){
            t2(function() {
                if (loop-- >= 0)
                    loop(cb);
                else
                    cb();
            });
        });
    })(function() {
        alert("finished!");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably not possible, but here goes: I want to create a struct
My question is probably not well phrased and it's probably a dupe but here
Okay that's probably not the best title, I know why we need browser resets:
Probably not the best title, but I'll explain: I have an array of objects
My subject line is probably not the best, but here's what I'm trying to
Not sure if the title explains itself, but probably not! Anyway, I have 50
this is probably not that hard to do. But I've got two text files.
The title is probably not accurate but I hope that reading this post you
Probably not the best title I've ever written, but I find it hard to
Probably not. But still would be nice to have it. Kinda hard to believe

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.