0

此功能在 Chrome 中有效,但在 IE 或 Safari 上无效,我做错了什么?

function countdown(){
    var dDay = new Date().getUTCDate() + 1;
    var dMonth = new Date().getUTCMonth() + 1;
    var dYear = new Date().getUTCFullYear();
    var BigDay = new Date(dYear+ ", " +dMonth+ ", " +dDay+ ",00:00:00");
    var msPerDay = 24 * 60 * 60 * 1000;
    var today = new Date();
            var timeLeft = (BigDay.getTime() - today.getTime());

            var e_daysLeft = timeLeft / msPerDay;
            var daysLeft = Math.floor(e_daysLeft);

            var e_hrsLeft = (e_daysLeft - daysLeft)*24;
            var hrsLeft = Math.floor(e_hrsLeft);

            var e_minsLeft = (e_hrsLeft - hrsLeft)*60;
            var minsLeft = Math.floor(e_minsLeft);

            var e_secsLeft = (e_minsLeft - minsLeft)*60;
            var secsLeft = Math.floor(e_secsLeft);
            if(daysLeft.toString().length === 1){
              daysLeft = "0"+daysLeft;
            } 
            if(hrsLeft.toString().length === 1){
              hrsLeft = "0"+hrsLeft;
            } 
            if(minsLeft.toString().length === 1){
              minsLeft = "0"+minsLeft;
            } 
            if(secsLeft.toString().length === 1){
              secsLeft = "0"+secsLeft;
            } 

            timeString = daysLeft + ":" + hrsLeft + ":" + minsLeft + ":" + secsLeft;
            return timeString;     
    }

我用它来打印计数器

window.setInterval(function(){
           $('#countdown').html("Time left: "+ countdown());
        }, 1000);

这是我在 Chrome 中得到的结果:

Time left: 00:01:55:15

这是我在 IE 和 Safari 中得到的结果:

Time left: NaN:NaN:NaN:NaN
4

1 回答 1

2

我认为是因为您正在构建一个无效的日期对象:

var BigDay = new Date(dYear+ ", " +dMonth+ ", " +dDay+ ",00:00:00");

文档:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

一个月表示:

表示月份的整数值,从 0 开始表示一月到 11 表示十二月。

您只想在今天添加一天,因此请尝试更改您的代码:

var BigDay = new Date();
BigDay.setDate(BigDay.getDate() + 1);
BigDay.setHours(0, 0, 0, 0);

演示:http: //jsfiddle.net/IrvinDominin/HgUhq/

于 2013-08-16T20:20:32.613 回答