Skip to content

Instantly share code, notes, and snippets.

@ywbsrp
Last active May 14, 2020 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ywbsrp/fb6b9c6f5850694f9e4f89c97eea61d5 to your computer and use it in GitHub Desktop.
Save ywbsrp/fb6b9c6f5850694f9e4f89c97eea61d5 to your computer and use it in GitHub Desktop.
document.addEventListener("DOMContentLoaded", function() {
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
//От какого времени начинаем отсчёт (по умолчанию - от текущего времени)
var now = new Date();
//До какого времени считаем (по умолчанию - до завтра)
var end = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 00, 00, 01);
var distance = end - now;
function showRemaining() {
var now = new Date();
var distance = end - now;
var hours = Math.floor((distance % _day) / _hour);
if (hours < 10) hours = '0' + hours;
var minutes = Math.floor((distance % _hour) / _minute);
if (minutes < 10) minutes = '0' + minutes;
var seconds = Math.floor((distance % _minute) / _second);
if (seconds < 10) seconds = '0' + seconds;
document.getElementById('h').innerText = (hours);
document.getElementById('m').innerText = (minutes);
document.getElementById('s').innerText = (seconds);
}
showRemaining();
setInterval(showRemaining, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment