gpt4 book ai didi

javascript - 倒计时器在 Safari 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 15:49:58 25 4
gpt4 key购买 nike

我有一个在 chrome 中工作得很好的倒计时器,但是当我在 safari 中查看时,它显示所有数字的 NAN,而且如果我设置过去的日期,它不会在 else 语句中触发我的模型。我在网上寻找解决方案,但没有找到。

  $(document).ready(function() {

$('#popupTimer').delay(1000).fadeIn(600);

// Set the date we're counting down to (*** Set to Apr 9th after testing ***)
var countDownDate = new Date("Apr 3, 2017 24:00:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get todays date and time
var now = new Date().getTime();

// Find the distance between now an 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="display"
document.getElementById("display").innerHTML = days + " Days " + hours + " Hours " + minutes + " Minutes " + seconds + " Seconds ";

// If the count down is finished,
if (distance < 0) {
clearInterval(x);
document.getElementById("display").innerHTML = "EXPIRED";
$('#myModal').modal('show');
$(".myDIV").hide();
$('#chooseProductThree').show();
$(".panel-heading").addClass("active-panel");
}
}, 1000);
});

最佳答案

首先写下函数

function parseDateString (dateString) {
var matchers = [];
matchers.push(/^[0-9]*$/.source);
matchers.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
matchers.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
matchers = new RegExp(matchers.join("|"));
if (dateString instanceof Date) {
return dateString;
}
if (String(dateString).match(matchers)) {
if (String(dateString).match(/^[0-9]*$/)) {
dateString = Number(dateString);
}
if (String(dateString).match(/\-/)) {
dateString = String(dateString).replace(/\-/g, "/");
}
return new Date(dateString);
} else {
throw new Error("Couldn't cast `" + dateString + "` to a date object.");
}
}

↓ ↓ ↓ ↓ ↓ then call this function like below ↓ ↓ ↓ ↓ ↓

var EndTime = "2019-05-10 00:00:00";
countDownDate=Date.parse(_self.parseDateString(EndTime));

关于javascript - 倒计时器在 Safari 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com