Search code examples
javascripttizen-web-app

javascript setInterval error checks


My issue is more related to tizen and the gear S2. I'm pulling weather from an API and I'm trying to do it every 15 minutes. Something is breaking somewhere, though, and I don't have access to console logs with this. My guess is it has something to do with when the screen is active vs. inactive. It works fine when trying from an internet browser.

var weatherVar = setInterval(runWeather, 900000);

function runWeather() {
    getLocation();
}

function getLocation() {
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
    } 
    else { 
        alert("Geolocation is not supported by this browser.");
    }
}

function showPosition(position) {
    var geoLat = position.coords.latitude,
        geoLong = position.coords.longitude,
        key = "my API key",
        WeatherURL = "http://api.wunderground.com/api/" + key + 
        "/conditions/q/" + geoLat + "," + geoLong + ".json";


    $.ajax({  
        url : WeatherURL,
        dataType : "jsonp",
        success : function(parsed_json) {
        var tempF = parsed_json['current_observation']['temp_f'];
        var weatherDescription = parsed_json['current_observation']['icon'];

        document.getElementById("currentTemp").innerHTML = tempF + '°';
        document.getElementById("condition").innerHTML = weatherDescription;
        }
    });
}

Edit, I had initially changed around some of the variables when posting this for some reason. Those have been fixed and were unrelated to the problems described.

Another method I had tried was comparing the current time to a timestamp pulled from the weather report. My time updates every half second, though, so I was pinging Weather Underground a hundred times during the lag time between initiating the call and when the weather report was generated. To fix that I created a variable and I would assign it a value of -1 when initiating the call. Then when the weather report was returned, it would change that value to a number based on the weather timestamp. This had issues, too, though, and I tried creating an error check that involved a second timer created anytime the variable changed to -1. It would wait a minute, see if it was still -1 and if so, it would try to run getLocation again. This caused me to ping WU too many times again, though. Any thoughts? Thanks!


Solution

  • Like with Chrome, the setInterval timer slows drastically when the screen is inactive. The problem was my error check also relied on an interval timer that was set with a relatively long interval. Since the timer that powers the time display is set for 500ms, I use that timer to compare the current time against the timestamp the weather report generates. When it's time to pull weather data, I've added a counter to increment each interval. If the weather time stamp doesn't update with a new number before x number of counters is generated then it will try pulling the data again.