should only grab once an hour now

This commit is contained in:
Martin Donnelly 2016-09-09 15:48:22 +01:00
parent 4ff2d0c759
commit 9b385f311d
3 changed files with 141 additions and 1 deletions

12
app/js/todayv2.js Normal file
View File

@ -0,0 +1,12 @@
/**
*
* User: Martin Donnelly
* Date: 2016-09-09
* Time: 15:11
*
*/
(function($) {
var clock = new Clock({model: new ClockModel()});
})(jQuery);

126
app/js/weatherclock.js Normal file
View File

@ -0,0 +1,126 @@
/**
*
* User: Martin Donnelly
* Date: 2016-09-09
* Time: 15:28
*
*/
'use strict';
/**
*
* User: Martin Donnelly
* Date: 2016-07-27
* Time: 09:23
*
*/
var WeatherModel = Backbone.Model.extend({
initialize: function() {
this.set('url','https://api.forecast.io/forecast/0657dc0d81c037cbc89ca88e383b6bbf/' + this.get('lat').toString() + ',' + this.get('long').toString() + '?units=uk2&exclude=minutely,hourly,daily,alerts,flags');
console.log(this.get('url'));
this.update();
},
update: function() {
this.getWeather();
var now = new Date;
var mod = 1800000 - (now.getTime() % 1800000);
var weatherTrigger = function() {
this.update();
};
setTimeout(weatherTrigger.bind(this), mod + 10);
},
getWeather: function() {
var self = this;
$.ajax({
type: 'GET',
url: self.get('url'),
data: '',
dataType: 'jsonp',
timeout: 10000,
context: $('body'),
contentType: ('application/json'),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
},
success: function(data) {
var stored = {
temperature: data.currently.temperature,
icon: data.currently.icon
};
self.set('data',stored);
},
error: function(xhr, type) {
console.error('ajax error');
console.error(xhr);
console.error(type);
}
});
}
});
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('<span class="hour">{24hr}</span>{mm}');
var curTime = now.format('<span class="time">{24hr} {mm}</span>');
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(
'<span class="wd-{do}">{Weekday}</span> <span class="mo mo-{M}">{Month} {dd}</span> {yyyy}'));
this.prevDate = curDate;
}
}
});
var Weather = Backbone.View.extend({
tagName: 'div',
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
this.$weatherText = $('#weatherText');
},
render: function() {
console.log('Weather:Render');
var data = this.model.get('data');
this.$weatherText.html(parseInt(data.temperature) + '&deg;c&nbsp;');
skycons.remove('icon1');
skycons.add('icon1', data.icon);
}
});

View File

@ -33,7 +33,9 @@ function runable() {
try{
var now = new Date().getTime();
if (now - todayCache.last < 360000) {
console.log('last updated',((now - todayCache.last) / 60000));
if (now - todayCache.last < 3600000) {
return false;
} else {
todayCache.last = now;