0

I have the following code:

var currentTemp = 0;
  $.simpleWeather({
    location: 'New York, New York',
    woeid: '',
    unit: 'f',
    success: function(weather) {
      currentTemp = weather.temp;
    },
    error: function(error) {
      currentTemp = 150;
    }
  });
  $('#degrees').text(currentTemp);

For some reason, the variable currentTemp's value, when set inside the simpleWeather function, doesn't carry outside of this function. What can I do?

AKor
  • 8,550
  • 27
  • 82
  • 136

1 Answers1

1

$('#degrees').text(currentTemp) doesn't wait for $.simplerWeather to fire up success handler. You should move whatever code you need to execute after the $.simplerWeather to the success handler (callback)

var currentTemp = 0;
  $.simpleWeather({
    location: 'New York, New York',
    woeid: '',
    unit: 'f',
    success: function(weather) {
      currentTemp = weather.temp;
      $('#degrees').text(currentTemp); 
    },
    error: function(error) {
      currentTemp = 150;
    }
  });
Adam Azad
  • 11,171
  • 5
  • 29
  • 70