【发布时间】:2021-08-04 18:58:05
【问题描述】:
我正在尝试制作多个计数器,它对我来说工作正常,但在某些浏览器上它显示 NAN 无效日期。我已经在我的 android 设备(chrome 和三星默认浏览器)上对其进行了测试,它可以正常工作,但我已经在 iPhone(chrome 和 safari)上对其进行了测试,但它无法正常工作。 我不确定我在代码中做错了什么,或者可能是我无法修复的兼容性问题。
这是我的小提琴 https://jsfiddle.net/infohassan/v4p5o7mq/1/
这是我的 JS
$(document).ready(function() {
var dt = new Date();
//Current Date
$('#date-1').attr('data-date', moment(dt).format("MM.D.YYYY HH:mm"));
// +2 Days
var dt2 = new Date();
var twoDays = dt2.setDate(dt2.getDate() + 2);
$('#date-2').attr('data-date', moment(dt2).format("MM.D.YYYY HH:mm"));
// +7 Days
var dt3 = new Date();
var twoDays = dt3.setDate(dt3.getDate() + 7);
$('#date-3').attr('data-date', moment(dt3).format("MM.D.YYYY HH:mm"));
$('.counter-sub').each(function(i, obj) {
var counterDate = $('.counter-sub label').eq(i).attr("data-date");
var countDownDate = new Date(counterDate).getTime();
$('.counter-sub label').eq(i).html(moment(countDownDate).format("D.MM.YYYY HH:mm"));
// Update the count down every 1 second
var x = setInterval(function() {
var now = new Date().getTime();
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);
var getOnlyHours = Math.floor((distance / (1000 * 60 * 60)));
var ShowTimer = days + " Day(s)";
if (i == 2) {
ShowTimer = ShowTimer;
} else {
ShowTimer = getOnlyHours + " Hours";
}
$('.counter-sub span').eq(i).html("Next to: " + ShowTimer);
// If the count down is over, write some text
if (distance < 0) {
//clearInterval(x);
days = days * -1;
hours = hours * -1;
minutes = minutes * -1;
seconds = seconds * -1;
getOnlyHours = getOnlyHours * -1;
ShowTimer = days + " Day(s)";
if (i == 2) {
ShowTimer = ShowTimer;
} else {
ShowTimer = getOnlyHours + " Hours";
}
$('.counter-sub span').eq(i).html("Over: " + ShowTimer);
}
}, 1000);
});
});
【问题讨论】:
标签: jquery jquery-mobile cross-browser