Skip to content

Instantly share code, notes, and snippets.

@httpsterio
Created May 4, 2017 11:18
Show Gist options
  • Select an option

  • Save httpsterio/18da63c6a5c584b15bb191722d121cc8 to your computer and use it in GitHub Desktop.

Select an option

Save httpsterio/18da63c6a5c584b15bb191722d121cc8 to your computer and use it in GitHub Desktop.
template-literal-test.js
let deadline = "05/05/2017 10:45";
function getTimeRemaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor(t / 1000 % 60);
var minutes = Math.floor(t / 1000 / 60 % 60);
var hours = Math.floor(t / (1000 * 60 * 60) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
total: t,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
};
}
function initializeClock(id, endtime) {
var clock = document.getElementById(id);
var timeinterval = setInterval(function() {
var t = getTimeRemaining(endtime);
clock.innerHTML =
`days: $t.days <br>
hours: $t.hours <br>
minutes: $t.minutes <br>
seconds: $t.seconds`;
if (t.total <= 0) {
clearInterval(timeinterval);
}
}, 1000);
}
initializeClock("clock", deadline);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment