【发布时间】:2019-10-10 05:54:27
【问题描述】:
我已经在我的站点中实现了以下代码,以显示一个正在运行的计时器。该网站在 Wordpress 上运行。目前日期是在代码中输入的(因此它适用于整个站点)。我希望在每个帖子上都有一个运行计时器。
我需要更改下面的代码,以便我可以在每个名为“到期”的帖子上使用 自定义字段 作为日期,而不是下面的硬连线日期 (newDate("Jan 2021 年 5 月 15:37:25).getTime()
<!-- Display the countdown timer in an element -->
<p id="demo"></p>
<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
以上代码来源于here
我的网站是here
提前致谢
【问题讨论】:
标签: javascript wordpress countdown custom-fields calculation