function weatherFailure() { Pebble.sendAppMessage({ PKJS_TEMP_BAR_COUNT: -1 }); } var xhrRequest = function (url, type, callback) { var xhr = new XMLHttpRequest(); xhr.timeout = 7500; xhr.onload = function () { if (xhr.status === 200) { callback(this.responseText); } else { weatherFailure(); } }; xhr.onerror = function () { weatherFailure(); }; xhr.ontimeout = function () { weatherFailure(); }; xhr.open(type, url); xhr.send(); }; function locationSuccess(pos) { var url = 'https://api.open-meteo.com/v1/forecast?' + 'latitude=' + pos.coords.latitude + '&longitude=' + pos.coords.longitude + '¤t=temperature_2m'; xhrRequest(url, 'GET', function (responseText) { const current_temp = JSON.parse(responseText).current.temperature_2m; const min_temp = JSON.parse(localStorage.getItem('clay-settings')).CLAY_TEMP_BAR_0; const max_temp = min_temp + 80; const clamped_temp = Math.min(Math.max(current_temp, min_temp), max_temp); const normalized_temp = clamped_temp - min_temp; const temp_bars = Math.round(normalized_temp / 4); Pebble.sendAppMessage({ PKJS_TEMP_BAR_COUNT: temp_bars }); } ); } function getWeather() { navigator.geolocation.getCurrentPosition( locationSuccess, weatherFailure, { timeout: 7500, maximumAge: 60000 } ); } Pebble.addEventListener("appmessage", function (dict) { if (dict.payload["PKJS_TEMP_BAR_COUNT"]) { getWeather(); } });