0

我正在使用http://keith-wood.name/countdown.html开发倒数计时器。

我的代码看起来像这样,

$('.clock').countdown({until:new Date(2012, 11, 31), format: 'odHMS', onTick:watchCountdown, tickInterval:5, layout:
    '<div id="timer">' +
        '<div id="timer_labels">'+
            '<div id="timer_months_label" class="timer_labels">Months</div>'+
            '<div id="timer_weeks_label" class="timer_labels">Days</div>'+
            '<div id="timer_days_label" class="timer_labels">Hours</div>'+
            '<div id="timer_minutes_label" class="timer_labels">Minutes</div>'+
        '</div>'+
        '<div id="timer_months" class="timer_numbers"><span class="divide"></span><span>{onn}</span></div>'+
        '<div id="timer_days" class="timer_numbers"><span class="divide"></span><span>{dnn}</span></div>'+
        '<div id="timer_hours" class="timer_numbers"><span class="divide"></span><span>{hnn}</span></div>'+
        '<div id="timer_minutes" class="timer_numbers"><span class="divide"></span><span>{mnn}</span></div>'+
    '</div>'
});

当你 onTick 我调用一个看起来像这样的函数,

function watchCountdown(periods) { 
var $div = $("#monitor");
var html = $div.html();
var checking = setInterval(function() {
    var newhtml = $div.html();
    if (html !== newhtml) {
        myCallback($div);
    }
},100);
$('#monitor').html('<div id="timer_minutes" class="timer_numbers"><span class="divide"></span><span class="minutes">' + periods[5] + '</span></div>'); 

}

在上面的函数中,检查了 html != newhtml 然而,尽管 #monitor 元素中有 HTML,但 html 却作为空变量 night 返回。我怎样才能让它意识到有要检查的内容?

4

1 回答 1

2

您不了解什么是 DOM 就绪,它只触发一次,当 DOM 完全加载并从服务器呈现时。

如果您使用 javascript 更改 DOM,它对 DOM 就绪事件没有影响。

$('#monitor').html('<div id="timer_minutes" class="timer_numbers"><span class="divide"></span><span class="minutes">' + periods[5] + '</span></div>');

上面的代码不会导致延迟或再次触发 DOM 就绪事件。

于 2012-06-27T10:27:52.137 回答