1

我似乎无法确定这段代码的问题,我正在挣扎。希望有更好的 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());
4

2 回答 2

4

observable 是 a function,因此您不能将其直接设置为更新后的值。相反,您必须调用它:

self.countdown(daysLeft + " : " + hrsLeft + " : " + minsLeft + " : " + secsLeft);

并在控制台中显示它你应该做

console.log(self.countdown());
于 2013-05-08T07:25:28.810 回答
-2

你应该看看 Knockout 可计算http://knockoutjs.com/documentation/computedObservables.html

于 2013-05-08T07:26:38.060 回答