繁体   English   中英

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

[英]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).readygetTime调用需要花费一秒钟以上的时间才能完成,这意味着您要在setInterval第一次执行之前对其进行设置。 因此, date变量尚未设置。

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

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

暂无
暂无

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

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