gpt4 book ai didi

javascript - JS 倒计时器(小时/秒)在 Safari 中不起作用

转载 作者:行者123 更新时间:2023-12-01 00:36:41 24 4
gpt4 key购买 nike

我有一个倒计时器,它在 Chrome 中运行得很好,但在 Safari 中却不行。我已经在 SO 上发现了类似的问题,但找不到适合我的解决方案。

如果我从时间戳中删除小时和秒,它也可以在 Safari 中工作,但我需要它的小时/分钟。

  function makeTimer() {
datestamp = "2019-09-25 00:00";
var endTime = new Date(datestamp);
// var endTime = new Date("2019-09-25 00:00");
endTime = (Date.parse(endTime) / 1000);

var now = new Date();
now = (Date.parse(now) / 1000);

var timeLeft = endTime - 7200 - now;

var days = Math.floor(timeLeft / 86400);
var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
var hours2 = Math.floor(((timeLeft - (days * 86400)) / 3600) + (days * 24));
var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600 )) / 60);
var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));

if (hours2 < "0") { hours2 = "00"; minutes = "0"; seconds = "0"; $(".body").addClass("timerOff"); } else if (hours2 < "10") { hours2 = "0" + hours2; }
if (minutes <= "0") { minutes = "00" } else if (minutes < "10") { minutes = "0" + minutes; }
if (seconds <= "0") { seconds = "00" } else if (seconds < "10") { seconds = "0" + seconds; }

$("#hours2").html(hours2);
$("#minutes").html(minutes);
$("#seconds").html(seconds);
}

setInterval(function() { makeTimer(); }, 500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<span id="hours2"></span>h
<span id="minutes"></span>m
<span id="seconds"></span>s

Fiddle

最佳答案

According to MDN :

A string value representing a date, specified in a format recognized by the Date.parse() method (these formats are IETF-compliant RFC 2822 timestamps and also strings in a version of ISO8601).

简而言之,datestamp = "2019-09-25 00:00" 是你的错误点。将日期更改为 2019-09-25T00:00:00Z 应该可以修复该问题。

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

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