๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_timers.asp
JavaScript Timers
Timers are browser APIs that schedule callback functions to run later. They are often used as simple examples of asynchronous JavaScript.
๐ŸŒ
W3Schools
w3schools.sinsixx.com โ€บ js โ€บ js_timing.asp.htm
JavaScript Timing Events
Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building.
๐ŸŒ
W3Schools
support.w3schools.com โ€บ hc โ€บ en-gb โ€บ articles โ€บ 4410409327761-How-do-I-create-a-countdown-timer
How do I create a countdown timer? โ€“ W3Schools.com
November 8, 2021 - Learn how to create a countdown timer with JavaScript. Learn how to do it in this tutorial: https://www.w3schools.com/howto/howto_js_countdown.asp Try it yourself here: https://www.w3school...
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ tryit.asp
Set and stop a timer with setInterval() and clearInterval()
The W3Schools online code editor allows you to edit code and view the result in your browser
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_async_timeouts.asp
JavaScript Timeouts
JavaScript timers let you call a function after a delay or at regular intervals.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ met_win_settimeout.asp
Window setTimeout() Method
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
W3Schools
w3schools.com โ€บ nodejs โ€บ nodejs_timers.asp
Node.js Timers Module
The Timers module provides functions that help schedule code execution at specific times or intervals. Unlike browser JavaScript, Node.js timing functions are provided as part of the Timers module, though they are available globally without ...
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ tryit.asp
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
๐ŸŒ
CodePen
codepen.io โ€บ pablosv โ€บ pen โ€บ LzNGwN
Javascript Countdown by W3Schools
Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor.
๐ŸŒ
W3Schools
w3schools.invisionzone.com โ€บ browser scripting โ€บ javascript
Help me make a countup timer (date) - JavaScript - W3Schools Forum
March 19, 2019 - Hey everyone! I could really use some help making my "countdown" timer count up by days, minutes, seconds. How do I modify the code below to accomplish this! Thanks again! // Set the date we're c...
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ met_win_setinterval.asp
Window setInterval() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ javascript-tutorial โ€บ javascript-timers.php
JavaScript Timer Functions - Tutorial Republic
For example, you can use timers to change the advertisement banners on your website at regular intervals, or display a real-time clock, etc. There are two timer functions in JavaScript: setTimeout() and setInterval().
๐ŸŒ
Javatpoint
javatpoint.com โ€บ javascript-timer
JavaScript timer - javatpoint
JavaScript timer with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, objects, map, typedarray etc.
Top answer
1 of 3
688

I have two demos, one with jQuery and one without. Neither use date functions and are about as simple as it gets.

Demo with vanilla JavaScript (version with a start/stop button here)

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds;

        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};
<body>
    <div>Registration closes in <span id="time">05:00</span> minutes!</div>
</body>

Demo with jQuery (version with a start/stop button here)

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.text(minutes + ":" + seconds);

        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}

jQuery(function ($) {
    var fiveMinutes = 60 * 5,
        display = $('#time');
    startTimer(fiveMinutes, display);
});

However if you want a more accurate timer that is only slightly more complicated: (version with a start/stop button here)

function startTimer(duration, display) {
    var start = Date.now(),
        diff,
        minutes,
        seconds;
    function timer() {
        // get the number of seconds that have elapsed since 
        // startTimer() was called
        diff = duration - (((Date.now() - start) / 1000) | 0);

        // does the same job as parseInt truncates the float
        minutes = (diff / 60) | 0;
        seconds = (diff % 60) | 0;

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds; 

        if (diff <= 0) {
            // add one second so that the count down starts at the full duration
            // example 05:00 not 04:59
            start = Date.now() + 1000;
        }
    };
    // we don't want to wait a full second before the timer starts
    timer();
    setInterval(timer, 1000);
}

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};
<body>
    <div>Registration closes in <span id="time"></span> minutes!</div>
</body>

Now that we have made a few pretty simple timers we can start to think about re-usability and separating concerns. We can do this by asking "what should a count down timer do?"

  • Should a count down timer count down? Yes
  • Should a count down timer know how to display itself on the DOM? No
  • Should a count down timer know to restart itself when it reaches 0? No
  • Should a count down timer provide a way for a client to access how much time is left? Yes

So with these things in mind lets write a better (but still very simple) CountDownTimer

function CountDownTimer(duration, granularity) {
  this.duration = duration;
  this.granularity = granularity || 1000;
  this.tickFtns = [];
  this.running = false;
}

CountDownTimer.prototype.start = function() {
  if (this.running) {
    return;
  }
  this.running = true;
  var start = Date.now(),
      that = this,
      diff, obj;

  (function timer() {
    diff = that.duration - (((Date.now() - start) / 1000) | 0);
        
    if (diff > 0) {
      setTimeout(timer, that.granularity);
    } else {
      diff = 0;
      that.running = false;
    }

    obj = CountDownTimer.parse(diff);
    that.tickFtns.forEach(function(ftn) {
      ftn.call(this, obj.minutes, obj.seconds);
    }, that);
  }());
};

CountDownTimer.prototype.onTick = function(ftn) {
  if (typeof ftn === 'function') {
    this.tickFtns.push(ftn);
  }
  return this;
};

CountDownTimer.prototype.expired = function() {
  return !this.running;
};

CountDownTimer.parse = function(seconds) {
  return {
    'minutes': (seconds / 60) | 0,
    'seconds': (seconds % 60) | 0
  };
};

So why is this implementation better than the others? Here are some examples of what you can do with it. Note that all but the first example can't be achieved by the startTimer functions.

An example that displays the time in XX:XX format and restarts after reaching 00:00

An example that displays the time in two different formats

An example that has two different timers and only one restarts

An example that starts the count down timer when a button is pressed

2 of 3
41

You can easily create a timer functionality by using setInterval() function. Below is the code which you can use it to create the timer.

http://jsfiddle.net/ayyadurai/GXzhZ/1/

window.onload = function() {
  var minute = 5;
  var sec = 60;
  setInterval(function() {
    document.getElementById("timer").innerHTML = minute + ":" + sec;
    sec--;

    if (sec == 00) {
      minute--;
      sec = 60;

      if (minute == 0) {
        minute = 5;
      }
    }
  }, 1000);
}
Registration closes in <span id="timer">5:00</span>!

๐ŸŒ
W3Schools
w3schools.invisionzone.com โ€บ browser scripting โ€บ javascript
Javascript Countdown Timer - JavaScript - W3Schools Forum
October 24, 2010 - I would like to take how many seconds are left from the php and post it into javascript and then have the javascript convert it into Hours Minutes Seconds, i would like it to auto refresh that function. On page refresh it automatically counts down the seconds, but I don't want to have to refresh ...