I need a knockout timer for my project which can restart after it reaches 0 on a click. I have the following code but this wont restar. Can somebody help me.
this.countDown = ko.observable();
ko.bindingHandlers.timer = {
update: function (element, valueAccessor) {
var sec = $(element).text();
var timer = setInterval(function () {
$(element).text(--sec);
if (sec == 0) {
clearInterval(timer);
}
}, 1000);
}
};
If you want to use the approach from your question replace this line:
with something like this:
See this at work:
However, I'd recommend encapsulating this kind of logic in the ViewModel, not in a custom binding. For example this kind of view model would work: