淘汰赛js倒计时器

Ana*_*nas 5 custom-binding knockout.js

我一直在寻找一个样本来为淘汰赛创建一个自定义倒数计时器绑定!

我发现了这个问题jQuery倒数计时器并适应Knockout Js.

Ana*_*nas 11

HTML代码:

<span data-bind="timer: $root.countDown">120</span>
Run Code Online (Sandbox Code Playgroud)

在viewModel中:define countDown

countDown: ko.observable()
Run Code Online (Sandbox Code Playgroud)

淘汰赛js自定义绑定:

ko.bindingHandlers.timer = {

    update: function (element, valueAccessor) {

        // retrieve the value from the span
        var sec = $(element).text();
        var timer = setInterval(function() { 

            $(element).text(--sec);
            if (sec == 0) {
                clearInterval(timer);
            }
        }, 1000);
    }
};
Run Code Online (Sandbox Code Playgroud)