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

  • Home
  • SEARCH
  • 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 6657969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:50:09+00:00 2026-05-26T01:50:09+00:00

I keep running into several issues when creating a countdown script it does not

  • 0

I keep running into several issues when creating a countdown script

  1. it does not run smoothly
  2. hard to make it repeat (closure)
  3. hard to delay the start and to delay the repeat (closure)

Can someone please help me FIX this code which should work in my opinion but doesn’t

the processing I need is

a. counter starts delay number of seconds after the page loads,
b. when counter reaches 0, the countdown RE-starts after delay number of seconds

Here is my Fiddle

Issues:

  1. when it starts, the counter seems to wait an additional second before counting down
  2. it does not pause
  3. the repeat starts after the counter has continued

.

// more accurate timer - https://gist.github.com/1185904
function interval(duration, fn){
  this.baseline = undefined

  this.run = function(){
    if(this.baseline === undefined){
      this.baseline = new Date().getTime()
    }
    fn()
    var end = new Date().getTime()
    this.baseline += duration

    var nextTick = duration - (end - this.baseline)
    if(nextTick<0){
      nextTick = 0
    }
    (function(i){
        i.timer = setTimeout(function(){
        i.run(end)
      }, nextTick)
    }(this))
  }

  this.stop = function(){
   clearTimeout(this.timer)
  }
}
window.onload=function() {
  var cnt1 = 10;
  var delay1 = 5;
  var timer1 = new interval(1000, function(){
    document.getElementById('out1').innerHTML=cnt1--
    if (cnt1 <= 0) { // trying to reset
      timer1.stop(); // does not work
      cnt1 = 10;
      setTimeout(function() { timer1.run()},delay1*1000)
    }
  })
  setTimeout(function() { timer1.run()},delay1*1000)
}  
  • 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-26T01:50:10+00:00Added an answer on May 26, 2026 at 1:50 am

    Object exposing start([delay]) and stop().

    http://jsfiddle.net/RVBDQ/3/

    function interval(duration, fn, delay){
        this.timer = null;
        this.duration = duration;
        this.fn = fn;
    
        this.start(delay);
    }
    interval.prototype.start = function(delay){
        if (this.timer) {return;}
        var self=this;
        this.timer = setTimeout(function(){ self.run(); }, delay||0);
    };
    interval.prototype.run = function(called){
        var self = this,
            nextTick = called ? this.duration - (new Date - called) : 0;
    
        this.timer = setTimeout(function(){
            self.fn();
            self.run(new Date);
        }, nextTick<0 ? 0 : nextTick);
    };
    interval.prototype.stop = function(){
        clearTimeout(this.timer);
        this.timer = null;
    };
    
    window.onload = function() {
        var cnt1 = 10;
        var delay1 = 5;
        window.timer1 = new interval(1000, function(){
            document.getElementById('out1').innerHTML=cnt1;
            cnt1 = cnt1 === 1 ? 10 : cnt1-1;
        }, delay1*1000);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep running into issues with a .bat script I want to write to
I keep running into this design problem, and I'm not happy with my solution
I keep running into enq: TX - row lock contention, when I run the
I keep having an error when running my web application. The error does not
I keep running into problems creating a Windows Azure Cloud Service and trying to
I am trying to run heroku rake db:migrate and keep running into the same
I keep running into this error MemCacheError (Broken pipe): Broken pipe on my Rails
Here's a problem I keep running into: I have a lot of situations where
I've been using RhinoMocks lately but I keep running into an issue. If I
I'm trying to work through the problems on projecteuler.net but I keep running into

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.