Skip to content

Instantly share code, notes, and snippets.

@XCanG
Created December 17, 2019 20:13
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 XCanG/d94a7dcae416d43bf3c5ee18651a03d1 to your computer and use it in GitHub Desktop.
Save XCanG/d94a7dcae416d43bf3c5ee18651a03d1 to your computer and use it in GitHub Desktop.
Обратный отсчёт
<div class="wrap">
Осталось
<div class="timer"></div>
</div>
const timer = document.querySelector(".timer"),
time = Date.UTC(2019, 11, 20, 3, 0, 0);
const update_timer = () => {
const dt = time - Date.now();
let t;
if (dt < 0) {
clearInterval(updating);
timer.innerText = "1";
} else {
const d = parseInt(dt / 86400000),
h = (parseInt(dt / 3600000) % 24).toString().padStart(2, "0"),
m = (parseInt(dt / 60000) % 60).toString().padStart(2, "0"),
s = (parseInt(dt / 1000) % 60).toString().padStart(2, "0"),
ms = (dt % 1000).toString().padStart(3, "0");
if (d > 0) {
let ds;
if (d >= 5) {
ds = `${d} дней`;
} else if (d >= 2) {
ds = `${d} дня`;
} else {
ds = `${d} день`;
}
t = `${ds} ${h}:${m}:${s}.${ms}`;
} else {
t = `${h}:${m}:${s}.${ms}`;
}
//console.log(t);
timer.innerText = t;
}
};
var updating = setInterval(update_timer, 100);
body {
min-height: 100vh;
oveflow: hidden;
background-color: #2D303A;
color: #ffffff;
text-shadow: 0 0 4px #000000;
font-family: 'Open Sans', sans-serif;
font-size: calc(5vh + 2vw);
font-weight: 600;
text-align: center;
}
.wrap {
width: 100vw;
height: 100vh;
display: grid;
align-content: center;
justify-content: center;
}
.timer {
font-weight: 300;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,600&amp;display=swap" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment