I am making a simple weather app with JavaScipt. My goal is when the user types in a location and the weather provider doesn't have that location, then the input box shakes (which is what loadWeatherError() does). The code below is running the else statement twice and then running the if code. This means that the loadWeatherError() function is being run twice and the input box shakes even if the location is valid. So when I run it I get the Error 2 alert twice then the Error 1 alert once. Is there a way that would only make the loadWeatherError() function only run once and run only if the weather provider didn't return the correct data?
xhr.onreadystatechange = function() {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK)
alert("Error 1");
var data = JSON.parse(xhr.responseText);
if (data.response) { //deal with wunderground api
} else { //deal with Yahoo api
}
} else {
alert("Error 2");
loadWeatherError();
options.error("There is a problem receiving the latest weather. Try again.");
}
};