/** * * User: Martin Donnelly * Date: 2016-10-03 * Time: 14:20 * */ var ClockModel = Backbone.Model.extend({ initialize: function() { this.set('now',new Date); this.update(); }, update: function() { var now = new Date; var mod = 60000 - (now.getTime() % 60000); this.set('now',now); var clockFn = function() { this.update(); }; setTimeout(clockFn.bind(this), mod + 10); } }); var Clock = Backbone.View.extend({ tagName: 'div', initialize: function() { _.bindAll(this, 'render'); this.model.bind('change', this.render); this.$date = $('#date'); this.$time = $('#time'); this.render(); }, render: function() { var now = this.model.get('now'); //var curTime = now.format('{24hr}{mm}'); var curTime = now.format('{24hr} {mm}'); var curDate = now.format('{yyyy}-{MM}-{dd}'); if (this.prevTime !== curTime) { this.$time.html(curTime); this.prevTime = curTime; } if (this.prevDate !== curDate) { this.$date.html(now.format( '{Weekday} {Month} {dd} {yyyy}')); this.prevDate = curDate; } } });