Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const days = document.getElementById("days");
- const hours = document.getElementById("hours");
- const minutes = document.getElementById("minutes");
- const seconds = document.getElementById("seconds");
- const input = document.getElementById("date");
- const show = document.getElementById("show");
- let intervalId
- const getCurrentDate = (from, to) => {
- return {
- remainingDays: Math.floor((from - to) / 1000 / 60 / 60 / 24),
- remainingHours: Math.floor((from - to) / 1000 / 60 / 60 % 24),
- remainingMinutes: Math.floor((from - to) / 1000 / 60 % 60),
- remainingSeconds: Math.floor((from - to) / 1000 % 60),
- }
- }
- const render = (date) => {
- const {
- remainingDays,
- remainingHours,
- remainingMinutes,
- remainingSeconds,
- } = date
- days.textContent = remainingDays +" д "
- hours.textContent = remainingHours +" ч "
- minutes.textContent = remainingMinutes +" м "
- seconds.textContent = remainingSeconds +" с "
- }
- const timer = (from) => {
- const to = Date.now()
- render(getCurrentDate(from, to))
- if (!intervalId) {
- intervalId = setInterval(() => timer(from), 1000);
- }
- if(
- Object.values(getCurrentDate(from, to)).every(param => !param)
- ) {
- clearInterval(intervalId)
- }
- }
- const countdownTimer = () => {
- const target = new Date(input.value)
- timer(target)
- }
- input.addEventListener("keydown", (e) => {
- if(e.keyCode < 48 || e.keyCode > 57) {
- e.preventDefault();
- }
- let len = input.value.length
- if(len == 4) {
- input.value = input.value + "."
- }
- if(len == 7) {
- input.value = input.value + "."
- }
- if(len == 10) {
- input.value = input.value + " / "
- }
- if(len == 15) {
- input.value = input.value + ":"
- }
- if(len == 18) {
- e.preventDefault();
- }
- });
- show.addEventListener("click", countdownTimer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement