简体   繁体   English

倒计时器在firefox中不起作用

[英]countdown timer doesn't work in firefox

Hi guys I have a countdown timer that is meant to run on a website. 嗨,大家好,我有一个倒计时器,打算在网站上运行。 Unfortunately it isn't supported in Firefox, though it runs fine in Chrome and Opera (other browsers yet to be checked). 不幸的是它在Firefox中不受支持,尽管它在Chrome和Opera(其他浏览器尚未检查)中运行良好。

The theory behind it is that a time stamp, "END", is set on a page and the saved in a DB (all working fine). 它背后的理论是在页面上设置时间戳“END”,并保存在DB中(所有工作正常)。 END is then retrieved (PHP) and fed into a jQuery function which counts down from the current time to END. 然后检索END(PHP)并将其输入jQuery函数,该函数从当前时间倒计时到END。

the php is all working as it should so my jQuery function is shown below as I'm sure it is located in here somewhere. php是全部工作,所以我的jQuery函数如下所示,因为我确定它位于这里的某个地方。

Thanks in advance guys! 先谢谢你们!

// Code for a countdown timer
var countdown = function(end, elements){
    var _second = 1000,
        _minute = _second*60,
        _hour = _minute*60,
        _day = _hour*24,

        finish = new Date(end),
        timer,

        calculate = function(){

            var now = new Date(),
                remaining = finish.getTime() - now.getTime(),
                data;

            if (isNaN(finish)){
                console.log("Invalid date/time");
                return;
            };

            if (remaining <= 0){
                clearInterval(timer);
                if (typeof callback ==='function'){
                    callback();
                }
            } else {
                if (!timer){
                    timer = setInterval(calculate, _second);
                };

                data = {
                    "days" : Math.floor(remaining/_day),
                    "hours" : Math.floor((remaining % _day) / _hour),
                    "minutes" : Math.floor((remaining % _hour) / _minute),
                    "seconds" : Math.floor((remaining % _minute) / _second)
                };

                if (elements.length){
                    for (x in elements){
                        var x = elements[x];
                        data[x] = ('00' + data[x]).slice(-2);
                        document.getElementById(x).innerHTML = data[x];
                    };
                };
            };
        };
    calculate();
}

sorry, it is then called as follows: 对不起,它被调用如下:

<script>
    var callback = function(){console.log('Done!')};
    countdown("<?php get_deadline($conn);?>", ['days', 'hours', 'minutes', 'seconds'], callback); // Get deadline for exam name
</script>

As I say the php function is working fine! 正如我所说的PHP功能正常!

I found the solution on another tread, located here 我在另一个位置找到了解决方案, 位于此处

All it took was to make the format readable across browsers as @charlietfl proposed. 所有这一切都是为了让浏览器的格式可以像@charlietfl那样提出。

the code I used was on line 8 of the original script: 我使用的代码是在原始脚本的第8行:

finish = new Date(end.replace(/\-/g,'\/').replace(/[T|Z]/g,' ')),
         timer,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM