silvrgit/app/js/clock.js

142 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-03-08 21:52:21 +00:00
/**
* Created by marti on 29/02/2016.
*/
(function() {
var storedData;
var weatherCount = 0;
2016-04-28 09:48:48 +00:00
var refreshTimer = 0;
var viewTimer = 0;
var eventBus = {};
2016-04-28 09:48:48 +00:00
MicroEvent.mixin(eventBus);
function getData() {
$.ajax({
type: 'GET',
url: '/today/data',
data: '',
dataType: 'json',
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) {
console.log(data);
storedData = data;
2016-06-06 13:15:47 +00:00
// startWeather();
2016-04-19 15:24:02 +00:00
updateWeather();
},
error: function(xhr, type) {
console.log("ajax error");
console.log(xhr);
console.log(type);
}
});
}
function updateWeather() {
$('#wCtext').empty().html(storedData.data.weather.currently);
$('#wLtext').empty().html(storedData.data.weather.later);
$('#wTtext').empty().html(storedData.data.weather.today);
// $('#wDaily').empty();
for (var t = 0; t < storedData.data.weather.data.daily.data.length; t++) {
var m = 'icon' + (t + 1).toString();
var d = '#d' + (t + 1).toString();
2016-10-05 14:08:57 +00:00
var w = '#w' + (t + 1).toString();
var ws = '<i class="wi wi-forecast-io-' + storedData.data.weather.data.daily.data[t].icon + '"></i>';
var ts = parseInt(storedData.data.weather.data.daily.data[t].time) * 1000;
2016-06-06 13:15:47 +00:00
var n = Date.create(ts).format('{Dow}');
2016-10-05 14:08:57 +00:00
$(w).empty().html(ws);
$(d).empty().html(n);
2016-03-08 21:52:21 +00:00
}
2016-05-22 22:36:31 +00:00
$('#wLater').hide();
$('#wToday').hide();
$('#wDaily').hide();
}
2016-03-08 21:52:21 +00:00
function switchWeather() {
2016-03-08 21:52:21 +00:00
weatherCount++;
weatherCount = weatherCount < 4 ? weatherCount : 0;
2016-03-08 21:52:21 +00:00
$('#wCurrent').toggle(weatherCount == 0);
$('#wLater').toggle(weatherCount == 1);
$('#wToday').toggle(weatherCount == 2);
$('#wDaily').toggle(weatherCount == 3);
2016-03-08 21:52:21 +00:00
}
2016-03-08 21:52:21 +00:00
// event bus
2016-03-08 21:52:21 +00:00
2016-04-28 09:48:48 +00:00
eventBus.bind('switchWeather', function() {
switchWeather();
});
2016-03-08 21:52:21 +00:00
// timers
2016-04-28 09:48:48 +00:00
function refreshWeatherView() {
eventBus.trigger('switchWeather');
var now = new Date();
2016-06-06 13:15:47 +00:00
var mod = 10000 - (now.getTime() % 10000);
viewTimer = setTimeout(function() {refreshWeatherView();}, mod + 10);
}
function updateDate() {
var now = new Date();
var n = Date.create(now).format('{Weekday}');
$('#day').html(n);
n = Date.create(now).format('{Month} {d}');
$('#date').html(n);
2016-03-08 21:52:21 +00:00
2016-06-06 13:15:47 +00:00
var mod = 60000 - (now.getTime() % 60000);
setTimeout(function() {updateDate();}, mod + 1);
2016-03-08 21:52:21 +00:00
}
2016-03-08 21:52:21 +00:00
function updateClock() {
2016-05-22 22:36:31 +00:00
var n = Date.create(new Date()).format('{HH}:{mm}:{ss}');
$('#clockDisplay').html(n);
}
2016-03-08 21:52:21 +00:00
function clock() {
'use strict';
updateClock();
2016-04-28 09:48:48 +00:00
var now = new Date();
2016-05-22 22:36:31 +00:00
//var mod = 60000 - (now.getTime() % 60000);
var mod = 1000 - (now.getTime() % 1000);
2016-03-08 21:52:21 +00:00
2016-05-22 22:36:31 +00:00
setTimeout(function() {clock();}, mod + 1);
2016-03-08 21:52:21 +00:00
}
2016-03-08 21:52:21 +00:00
function refresh() {
2016-04-28 09:48:48 +00:00
getData();
var now = new Date();
2016-05-26 21:29:21 +00:00
var mod = 1800000 - (now.getTime() % 1800000);
2016-04-28 09:48:48 +00:00
refreshTimer = setTimeout(function() {refresh();}, mod + 10);
}
2016-06-06 13:15:47 +00:00
updateDate();
clock();
2016-04-28 09:48:48 +00:00
// getData();
refresh();
2016-04-28 09:48:48 +00:00
refreshWeatherView();
2016-06-06 13:15:47 +00:00
// $('#misc').html($(window).width());
2016-03-08 21:52:21 +00:00
})();