/** * Created by marti on 29/02/2016. */ (function () { var storedData; var self = this; var weatherCount = 0; var skycons = new Skycons({"color": "white"}); MicroEvent.mixin(this); 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; startWeather(); }, 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(); var ts = parseInt(storedData.data.weather.data.daily.data[t].time) * 1000; var n = Date.create(ts).format('{weekday}'); skycons.add(m, storedData.data.weather.data.daily.data[t].icon); $(d).empty().html(n); } skycons.play(); $('#wLater').hide(); $('#wToday').hide(); $('#wDaily').hide(); } function switchWeather() { weatherCount++; weatherCount = weatherCount < 4 ? weatherCount : 0; $('#wCurrent').toggle(weatherCount == 0); $('#wLater').toggle(weatherCount == 1); $('#wToday').toggle(weatherCount == 2); $('#wDaily').toggle(weatherCount == 3); } // event bus this.bind('switchWeather', function () { switchWeather(); }); // timers function startWeather() { updateWeather(); setInterval(function () { self.trigger('switchWeather'); }, 10000); } function startClock() { setInterval(function () { var n = Date.create(new Date()).format('{hh}:{mm}:{ss}'); $('#clockDisplay').html(n); }, 1000); } function refresh() { setInterval(function () { getData() }, 900000); } startClock(); getData(); refresh(); })();