mercredi 5 avril 2017

JQuery weather app - print text w if statements?

I'm new to jQuery, and I'm trying to write a web app to tell you the weather in words (as well as numbers.) I'm using the simpleweather jQuery plugin, which uses Yahoo's weather info. The docs show a "weather code" associated with each "type" of weather.

I'd like to implement an if statement that would print a custom string of text depending on the given weather code.

My instincts say something like this:

if(weather.code = 0) {
html += '<p>'"There's a tornado!"'</p>';
}

But I also figure I should create a function that holds the associated strings, so that my code isn't just a huge mess.

function(printWeather) {
if(weather.code = 0) {
"There's a tornado!"
}

and then in my loadWeather function include something like:

html += '<p>'+printWeather+'</p>';

Here's my loadWeather function as it is now:

function loadWeather(location, woeid) {
 $.simpleWeather({
  location: location,
  woeid: woeid,
  unit: 'f',
  success: function(weather) {
   if(weather.temp > 75) {
    $('body').animate({backgroundColor: '#F7AC57'}, 1500);
  } else {
    $('body').animate({backgroundColor: '#0091c2'}, 1500);
  }
  html = '<h2><i class="icon-'+weather.code+'"></i> '+weather.temp+'&deg;'+weather.units.temp+'</h2>';
  html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
  html += '<li class="currently">'+weather.currently+'</li>';
  html += '<li>'+weather.alt.temp+'&deg;C</li></ul>';  

  $("#weather").html(html);
},
error: function(error) {
  $("#weather").html('<p>'+error+'</p>');
}
});

Would appreciate any guidance! Thank you.

Aucun commentaire:

Enregistrer un commentaire