倒数计时器在Safari中不起作用

时间:2017-04-04 23:57:11

标签: javascript jquery

我有一个倒计时器,它在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);
    });

4 个答案:

答案 0 :(得分:0)

我认为safari无法解析此行中的日期:

var countDownDate = new Date("Apr 3, 2017 24:00:00").getTime();

更改为:

var countDownDate = Date.parse("Apr 3, 2017 24:00:00");

答案 1 :(得分:0)

首先在下方写入功能

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.");
            }
        }
  

↓↓↓↓↓然后按如下所示调用此功能↓↓↓↓↓

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

答案 2 :(得分:0)

我遇到了同样的问题,这是我用以下方法解决的问题:

<script type="text/javascript">
    const second = 1000;
    const minute = second * 60;
    const hour = minute * 60;
    const day = hour * 24;

    // Have to split time funny for IOS and Safari NAN and timezone bug
    var timeParsed = '{{ $startTime }}'.replace(' ', 'T').split(/[^0-9]/);
    var countDown = new Date(new Date (timeParsed[0],timeParsed[1]-1,timeParsed[2],timeParsed[3],timeParsed[4],timeParsed[5])).getTime();

    let x = setInterval(function() {
        let now = new Date().getTime();
        let distance = countDown - now;
        if(Math.floor(distance / (day)) > 0) {
            document.getElementById("days_line").style.display = "inline-block";
        } else {
            document.getElementById("days_line").style.display = "none";
        }
        document.getElementById('days').innerText = Math.floor(distance / (day));
        document.getElementById('hours').innerText = Math.floor((distance % (day)) / (hour));
        document.getElementById('minutes').innerText = Math.floor((distance % (hour)) / (minute));
        document.getElementById('seconds').innerText = Math.floor((distance % (minute)) / second);
        if (distance < 0) {
            clearInterval(x);
            $('.counter-container').fadeOut(function() {
                $('.counter-message').fadeIn();
            });
        } else {
            $('.counter-container').fadeIn();
        }
    }, second)
</script>

注释{{ startTime }}不是javascript,而是从Blade导入的PHP。只需在此处输入您的约会日期即可。

答案 3 :(得分:0)

刚刚遇到了这个问题,并使用以下日期格式进行了修复(请注意:“ /”而不是用“ -”分隔日期,月份和年份...

"2019/05/10T00:00:00Z";