以下代码在桌面版Chrome上完美运行,但未在移动Chrome(iPhone)上正确返回倒计时。也在Safari中检查,结果相同。这是一个简单的jQuery
倒计时。自从innerHTML
设置完毕后,脚本本身就会运行,只是没有正确的数据。另外,我确认getTime()
也是正确的。可能有什么区别?
桌面
$(document).ready(function() {
$(".cd").each(function(index, obj) {
var countDownDate = new Date(obj.getAttribute('value'));
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
obj.innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
obj.innerHTML = "EXPIRED";
}
}, 1000);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="demo" class="cd" value="2018-06-10 18:00:00"></p>
<p id="demo2" class="cd" value="2018-06-11 18:00:00"></p>
答案 0 :(得分:0)
这适用于我的Chrome iPhone - 你的不是。
还在Windows Chrome上进行了测试。有关此格式2011-11-24T09:00:27+0000
$(document).ready(function() {
$(".cd").each(function(index, obj) {
var countDownDate = new Date(obj.getAttribute('value').replace(/-/g, "/"));
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
obj.innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
obj.innerHTML = "EXPIRED";
}
}, 1000);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="demo" class="cd" value="2018-06-10 18:00:00"></p>
<p id="demo2" class="cd" value="2018-06-11 18:00:00"></p>