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

标签 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);
    });

最佳答案

首先写下函数

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/

相关文章:

jquery - 添加:hover selector to class 1 when hovering over class 2

javascript - 获取从一个页面到另一页面的点击 href 变量

javascript - 颜色叠加在不透明的div上

javascript - 以编程方式预先选择 Material-UI 数据网格中的一行(React)

javascript - 在 HTML5 中从 Canvas 中删除图像

javascript - 在 Shadow DOM 内部时,Polymer 元素可以向其父元素添加事件监听器吗?

自动滚动到不断增长的页面底部的 Javascript 机制?

javascript - 如何检测 Durandal 中的后退按钮点击

Jquery:将最后一行集中在文本区域的更改上

javascript - 使用 PHP 和 AJAX 发送电子邮件表单