我似乎无法确定这段代码的问题,我正在挣扎。希望有更好的 Javascript 印章的人可以看到我做错了什么。Chrome 控制台显示每秒更新的值,但它不会绑定在视图中。我在http://jsfiddle.net/PLSMW/7/有一个 jsfiddle
html绑定很简单:
js文件:
function viewModel() {
var self = this;
self.BigDay = new Date("03 Jun 2013, 14:30:00");
self.msPerDay = 24 * 60 * 60 * 1000;
self.countdown = ko.observable("24:00:00:00");
self.tick = function () {
var today = new Date();
var timeLeft = (self.BigDay.getTime() - today.getTime());
var e_daysLeft = timeLeft / self.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);
self.countdown = daysLeft + " : " + hrsLeft + " : " + minsLeft + " : " + secsLeft;
console.log(self.countdown);
};
setInterval(self.tick, 1000);
}
ko.applyBindings(new viewModel());