[jquery] Code for a simple JavaScript countdown timer?

For the sake of performances, we can now safely use requestAnimationFrame for fast looping, instead of setInterval/setTimeout.

When using setInterval/setTimeout, if a loop task is taking more time than the interval, the browser will simply extend the interval loop, to continue the full rendering. This is creating issues. After minutes of setInterval/setTimeout overload, this can freeze the tab, the browser or the whole computer.

Internet devices have a wide range of performances, so it's quite impossible to hardcode a fixed interval time in milliseconds!

Using the Date object, to compare the start Date Epoch and the current. This is way faster than everything else, the browser will take care of everything, at a steady 60FPS (1000 / 60 = 16.66ms by frame) -a quarter of an eye blink- and if the task in the loop is requiring more than that, the browser will drop some repaints.

This allow a margin before our eyes are noticing (Human = 24FPS => 1000 / 24 = 41.66ms by frame = fluid animation!)

https://caniuse.com/#search=requestAnimationFrame

_x000D_
_x000D_
/* Seconds to (STRING)HH:MM:SS.MS ------------------------*/_x000D_
/* This time format is compatible with FFMPEG ------------*/_x000D_
function secToTimer(sec){_x000D_
  const o = new Date(0), p =  new Date(sec * 1000)_x000D_
  return new Date(p.getTime()-o.getTime()).toString().split(" ")[4] + "." + p.getMilliseconds()_x000D_
}_x000D_
_x000D_
/* Countdown loop ----------------------------------------*/_x000D_
let job, origin = new Date().getTime()_x000D_
const timer = () => {_x000D_
  job = requestAnimationFrame(timer)_x000D_
  OUT.textContent = secToTimer((new Date().getTime() - origin) / 1000)_x000D_
}_x000D_
_x000D_
/* Start looping -----------------------------------------*/_x000D_
requestAnimationFrame(timer)_x000D_
_x000D_
/* Stop looping ------------------------------------------*/_x000D_
// cancelAnimationFrame(job)_x000D_
_x000D_
/* Reset the start date ----------------------------------*/_x000D_
// origin = new Date().getTime()
_x000D_
span {font-size:4rem}
_x000D_
<span id="OUT"></span>_x000D_
<br>_x000D_
<button onclick="origin = new Date().getTime()">RESET</button>_x000D_
<button onclick="requestAnimationFrame(timer)">RESTART</button>_x000D_
<button onclick="cancelAnimationFrame(job)">STOP</button>
_x000D_
_x000D_
_x000D_

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to timer

Flutter Countdown Timer How to do a timer in Angular 5 Create a simple 10 second countdown Python loop to run for certain amount of seconds Error 1053 the service did not respond to the start or control request in a timely fashion Wait some seconds without blocking UI execution The simplest possible JavaScript countdown timer? How can I perform a short delay in C# without using sleep? How to add a "sleep" or "wait" to my Lua Script? How to use timer in C?

Examples related to counter

HTML/Javascript Button Click Counter How to sort Counter by value? - python How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace? Counter increment in Bash loop not working Get loop counter/index using for…of syntax in JavaScript Count characters in textarea Counter in foreach loop in C# jQuery counter to count up to a target number How to count the frequency of the elements in an unordered list? Code for a simple JavaScript countdown timer?

Examples related to setinterval

How to make `setInterval` behave more in sync, or how to use `setTimeout` instead? How can I pause setInterval() functions? Can clearInterval() be called inside setInterval()? Stop setInterval clearInterval() not working Calculating Page Load Time In JavaScript Javascript setInterval not working How to start and stop/pause setInterval? How do I reset the setInterval timer? jquery function setInterval

Examples related to clearinterval

Can clearInterval() be called inside setInterval()? clearInterval() not working Code for a simple JavaScript countdown timer?