获取错误无法读取属性' getTime'未定义的

时间:2015-06-24 14:14:30

标签: javascript jquery countdown

当我尝试设置每天的倒计时时,我遇到了这个错误。 这是我的剧本:

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);

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

1 个答案:

答案 0 :(得分:1)

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

要解决此问题,请在初始化回调函数中的setInterval变量后移动date调用,以确保在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;
    })());
}