Offline Knockout Display Update

How to display and update time without user-generated event? Here is my beginning on it, but I cannot figure out how to get an idea for the update.

function viewModel() { this.clock = ko.computed(function() { return new Date().getTime(); }, this).extend({ throttle: 1000 }); }; ko.applyBindings(new viewModel()); 

with the following HTML:

 <span data-bind="text: clock"></span> 

Here's the jsfiddle .

+7
source share
2 answers

This seemed to be a trick:

 function viewModel() { var self = this; this.clock = ko.observable(new Date()); this.tick = function() { self.clock(new Date()); }; setInterval(self.tick, 3000); }; ko.applyBindings(new viewModel()); 

combined with this HTML:

 <span data-bind="text: clock"></span> 
+19
source

 ko.now = function(interval) { var now = ko.observable(); var time = new Date().getTime() setTimeout(function() { setInterval(function() { now(new Date()); }, interval); now(new Date()); }, interval - time % interval); now(new Date(time - time % interval)); return now; } ko.applyBindings({ now: ko.now(5*1000) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <div id="clock" data-bind="text: now"></div> 
0
source

All Articles