出现错误 Cannot read 属性 'getTime' of undefined

Getting error Cannot read property 'getTime' of undefined

我在尝试为每天设置倒计时时遇到此错误。 这是我的脚本:

var date;
var display = document.getElementById('time');

$(document).ready(function() {
    getTime('GMT', function(time){
        date = new Date(time);
    });    
});

setInterval(function() {
    date = new Date(date.getTime() + 1000);

    var currenthours = date.getHours();
    var hours;
    var minutes;
    var seconds;
    if (currenthours != 21){
        if (currenthours < 21) {
            hours = 20 - currenthours;
        } else {
            hours = 21 + (24 - currenthours);
        }
        minutes = 60 - date.getMinutes();
        seconds = 60 - date.getSeconds();

        if (minutes < 10) {
            minutes = '0' + minutes;
        }
        if (seconds < 10) {
            seconds = '0' + seconds;
        }

        display.innerHTML = hours + ':' + minutes + ':' +seconds;
     } else { 
        display.innerHTML = 'LIVE NOW';
    }
}, 1000);

function getTime(zone, success) {
    var url = 'http://json-time.appspot.com/time.json?tz=' + zone,
        ud = 'json' + (+new Date());
    window[ud]= function(o){
        success && success(new Date(o.datetime));
    };
    document.getElementsByTagName('head')[0].appendChild((function(){
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = url + '&callback=' + ud;
        return s;
    })());
}

导致此错误的行是

date = new Date(date.getTime() + 1000);

基本上我想做的是创建一个每天重置的倒计时。

$(document).ready 中的 getTime 调用需要超过一秒才能完成,这意味着您在它执行之前设置的 setInterval。因此,date 变量尚未设置。

要解决此问题,请在回调函数中初始化 date 变量后将 setInterval 调用移至 ,以确保您的 date 始终设置在 setInterval 之前函数执行。

var date;
var display = document.getElementById('time');

$(document).ready(function() {
    getTime('GMT', function(time){
        date = new Date(time);

        setInterval(function() {
            date = new Date(date.getTime() + 1000);

            var currenthours = date.getHours();
            var hours;
            var minutes;
            var seconds;
            if (currenthours != 21){
                if (currenthours < 21) {
                    hours = 20 - currenthours;
                } else {
                    hours = 21 + (24 - currenthours);
                }
                minutes = 60 - date.getMinutes();
                seconds = 60 - date.getSeconds();

                if (minutes < 10) {
                    minutes = '0' + minutes;
                }
                if (seconds < 10) {
                    seconds = '0' + seconds;
                }

                display.innerHTML = hours + ':' + minutes + ':' +seconds;
           } else { 
                display.innerHTML = 'LIVE NOW';
           }
        }, 1000);
    });    
});

function getTime(zone, success) {
    var url = 'http://json-time.appspot.com/time.json?tz=' + zone,
        ud = 'json' + (+new Date());
    window[ud]= function(o){
        success && success(new Date(o.datetime));
    };
    document.getElementsByTagName('head')[0].appendChild((function(){
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = url + '&callback=' + ud;
        return s;
    })());
}