javascript - 加载后 JavaScript 和内容更改 - 从日期开始的倒数计时器
问题描述
我正在尝试使用一个简单的倒计时计时器,将页面上的时间转换为倒计时。
它可以工作,但我当前的问题是如何显示正常日期,然后再由JavaScript 解析。我希望它立即由 JS 解析,这样用户就不会看到它在日期和倒数计时器之间闪烁。
它将其转换为倒计时:
<span class="countdown">12/10/20 13:10:00</span>
这是代码:
if ($('.countdown').length)
{
$.each( $('.countdown'), function( key, value )
{
var time_listed = $(value).text();
var countdown_object = $(value);
// Set the date we're counting down to
var countDownDate = new Date(time_listed).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"
countdown_object.text (days + " days " + hours + "h "
+ minutes + "m " + seconds + "s ");
// If the count down is finished, write some text
if (distance < 0)
{
clearInterval(x);
countdown_object.text("EXPIRED");
}
}, 1000);
});
}
我要的是关于如何解决这个问题的建议。是在 HTML 之前加载它的唯一方法还是什么?我对此的最佳实践感到困惑。到处都在告诉我推迟 JavaScript 加载……但是像这样改变内容的东西呢?
在这种情况下,是否有一个“核心”文件用于立即加载内容更改的内容,然后在内容之后加载其余内容或什么?
解决方案
问题来自 setInterval 没有自动执行,这是正常的。这是一个解决方法:
if ($('.countdown').length)
{
$.each( $('.countdown'), function( key, value )
{
var time_listed = $(value).text();
var countdown_object = $(value);
// Set the date we're counting down to
var countDownDate = new Date(time_listed).getTime();
var counterFunction = 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"
countdown_object.text (days + " days " + hours + "h "
+ minutes + "m " + seconds + "s ");
// If the count down is finished, write some text
if (distance < 0)
{
clearInterval(x);
countdown_object.text("EXPIRED");
}
return counterFunction;
}
// Update the count down every 1 second
var x = setInterval(counterFunction(), 1000);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="countdown">12/10/20 13:10:00</span>
推荐阅读
- javascript - 多个模式按钮不适用于 JavaScript
- node.js - Mongoose 计数文档,其中属性设置为 false 的数组元素的计数小于一个值
- android - Android clipRect 未按预期剪辑
- python - 如何将文本文件中的数据提取到定义为空白行之间的数据行的句子中?
- css - 在coldfusion中的日期比较中显示更多/显示更少
- html - 我怎样才能对齐
- expo - 导入自己的 tensorflow 模型来响应原生
- javascript - Firestore 中断查询
- git - 从 EC2 添加 Git 和复制文件
- r - 在 R 中,如何从自定义打印函数可靠地打印对象的名称?