重置倒计时
Reset Countdown
我找到了适合我的网络项目的倒计时。我希望每周五上午 9 点重新开始倒计时。有人可以给我提示吗?这是代码:
(function(){
const days = document.getElementById("days");
const hours = document.getElementById("hours");
const minutes = document.getElementById("minutes");
const seconds = document.getElementById("seconds");
const currentDate = new Date().getFullYear();
const concertDate = new Date(`June 19 ${currentDate} 09:00:00`);
function updateCountdown() {
const currentTime = new Date();
const diff = concertDate - currentTime;
const d = Math.floor(diff / 1000 / 60 / 60 / 24);
const h = Math.floor(diff / 1000 / 60 / 60) % 24;
const m = Math.floor(diff / 1000 / 60) % 60;
const s = Math.floor(diff / 1000) % 60;
days.innerHTML = d;
hours.innerHTML = h < 10 ? "0" + h : h;
minutes.innerHTML = m < 10 ? "0" + m : m;
seconds.innerHTML = s < 10 ? "0" + s : s;
}
setInterval(updateCountdown, 1000);
})();
您可以使用 [cron]:https://www.npmjs.com/package/cron;
你的 cron 时间应该是 0 9 * * 5
这是我的做法。在倒计时函数中,添加一个 if 语句来检查是否 diff <= 0
- 如果是(倒计时已过期),只需将一周的时间添加到 concertDate,新的倒计时开始。
为此,首先您必须将 concertDate
声明更改为 let concertDate
以允许稍后更改它。而且,您需要为音乐会使用 .getTime()
,它将以毫秒为单位为您提供该日期的时间(这使得在最后添加一周的时间成为可能)。
(function(){
const days = document.getElementById("days");
const hours = document.getElementById("hours");
const minutes = document.getElementById("minutes");
const seconds = document.getElementById("seconds");
const currentDate = new Date().getFullYear();
let concertDate = new Date(`June 19 ${currentDate} 09:00:00`).getTime();
function updateCountdown() {
const currentTime = new Date();
const diff = concertDate - currentTime;
const d = Math.floor(diff / 1000 / 60 / 60 / 24);
const h = Math.floor(diff / 1000 / 60 / 60) % 24;
const m = Math.floor(diff / 1000 / 60) % 60;
const s = Math.floor(diff / 1000) % 60;
days.innerHTML = d;
hours.innerHTML = h < 10 ? "0" + h : h;
minutes.innerHTML = m < 10 ? "0" + m : m;
seconds.innerHTML = s < 10 ? "0" + s : s;
if (diff <= 0) {
concertDate = concertDate + (1000 * 3600 * 24 * 7); //add one week to concert date
}
}
setInterval(updateCountdown, 1000);
})();
希望对您有所帮助!
我找到了适合我的网络项目的倒计时。我希望每周五上午 9 点重新开始倒计时。有人可以给我提示吗?这是代码:
(function(){
const days = document.getElementById("days");
const hours = document.getElementById("hours");
const minutes = document.getElementById("minutes");
const seconds = document.getElementById("seconds");
const currentDate = new Date().getFullYear();
const concertDate = new Date(`June 19 ${currentDate} 09:00:00`);
function updateCountdown() {
const currentTime = new Date();
const diff = concertDate - currentTime;
const d = Math.floor(diff / 1000 / 60 / 60 / 24);
const h = Math.floor(diff / 1000 / 60 / 60) % 24;
const m = Math.floor(diff / 1000 / 60) % 60;
const s = Math.floor(diff / 1000) % 60;
days.innerHTML = d;
hours.innerHTML = h < 10 ? "0" + h : h;
minutes.innerHTML = m < 10 ? "0" + m : m;
seconds.innerHTML = s < 10 ? "0" + s : s;
}
setInterval(updateCountdown, 1000);
})();
您可以使用 [cron]:https://www.npmjs.com/package/cron;
你的 cron 时间应该是 0 9 * * 5
这是我的做法。在倒计时函数中,添加一个 if 语句来检查是否 diff <= 0
- 如果是(倒计时已过期),只需将一周的时间添加到 concertDate,新的倒计时开始。
为此,首先您必须将 concertDate
声明更改为 let concertDate
以允许稍后更改它。而且,您需要为音乐会使用 .getTime()
,它将以毫秒为单位为您提供该日期的时间(这使得在最后添加一周的时间成为可能)。
(function(){
const days = document.getElementById("days");
const hours = document.getElementById("hours");
const minutes = document.getElementById("minutes");
const seconds = document.getElementById("seconds");
const currentDate = new Date().getFullYear();
let concertDate = new Date(`June 19 ${currentDate} 09:00:00`).getTime();
function updateCountdown() {
const currentTime = new Date();
const diff = concertDate - currentTime;
const d = Math.floor(diff / 1000 / 60 / 60 / 24);
const h = Math.floor(diff / 1000 / 60 / 60) % 24;
const m = Math.floor(diff / 1000 / 60) % 60;
const s = Math.floor(diff / 1000) % 60;
days.innerHTML = d;
hours.innerHTML = h < 10 ? "0" + h : h;
minutes.innerHTML = m < 10 ? "0" + m : m;
seconds.innerHTML = s < 10 ? "0" + s : s;
if (diff <= 0) {
concertDate = concertDate + (1000 * 3600 * 24 * 7); //add one week to concert date
}
}
setInterval(updateCountdown, 1000);
})();
希望对您有所帮助!