0

I'm doing this Local Weather App for a FreeCodeCamp challenge. You can check my project over codepen at: https://codepen.io/Thiagoral/pen/jVjqwo

I get a JSON from DarkSky, then I store one of the ten possible weather conditions (clear-day, rain, snow etc) in the following fashion:

window.little_icon = json.currently.icon;

Outside the getJSON function, I'm trying to create a switch, in which I evaluate the window.little_icon key and set an icon to the canvas, according to the proper string value.

var icons = new Skycons({color: "#fff"});    

switch(window.little_icon) {
  case "clear-day":
  $("canvas").attr("id", "clear-day");
  icons.set("clear-day", Skycons.CLEAR_DAY);
  break;

  case "clear-night":
  $("canvas").attr("id", "clear-night");
  icons.set("clear-night", Skycons.CLEAR_NIGHT);
  break;

  case "rain":
  $("canvas").attr("id", "rain");
  icons.set("rain", Skycons.RAIN);
  break;

  case "snow":
  $("canvas").attr("id", "snow");
  icons.set("snow", Skycons.SNOW);
  break;

  case "sleet":
  $("canvas").attr("id", "sleet");
  icons.set("sleet", Skycons.SLEET);
  break;

  case "wind":
  $("canvas").attr("id", "wind");
  icons.set("wind", Skycons.WIND);
  break;

  case "fog":
  $("canvas").attr("id", "fog");
  icons.set("fog", Skycons.FOG);
  break;  

  case "cloudy":
  $("canvas").attr("id", "cloudy");
  icons.set("cloudy", Skycons.CLOUDY);
  break;  

  case "partly-cloudy-day":
  $("canvas").attr("id", "partly-cloudy-day");
  icons.set("partly-cloudy-day", Skycons.PARTLY_CLOUDY_DAY);
  break;

  case "partly-cloudy-night":
  $("canvas").attr("id", "partly-cloudy-night");
  icons.set("partly-cloudy-night", Skycons.PARTLY_CLOUDY_NIGHT);
  break;

}

icons.play();

This is not working though for some reason. I used the same tactic a little before, when I got the temperature inside the getJSON function with

window.temperature = Math.round(json.currently.temperature);

and I accessed it outside, when I was trying to do the Celsius/Fahrenheit conversion. I had no trouble accessing this temperature value outside its original function, but the same is not working with the json.currently.icon value.

If I create the switch inside the getJSON function, as exposed below, the icons display correctly.

  // Only the weather state and the temperature, whose unit depends on the client region, are inserted in their respective html div tags. The temperature is rounded down to eliminate decimal places
  $.getJSON(url,function(json){

    //Assigns the temperature to the window object, so it can be accessed outside of getJSON
   window.temperature = Math.round(json.currently.temperature);

    $("#weather").html(json.currently.summary);     
    $("#temperature").html(window.temperature);

    // Sets the background picture according to the weather described in icon
    var little_icon = json.currently.icon;
    // window.aff = json.currently.icon; 
    $("body").addClass(little_icon);

    // Skycon is set according to the icon stored in the json
var icons = new Skycons({color: "#fff"});    

switch(little_icon) {
  case "clear-day":
  $("canvas").attr("id", "clear-day");
  icons.set("clear-day", Skycons.CLEAR_DAY);
  break;

  case "clear-night":
  $("canvas").attr("id", "clear-night");
  icons.set("clear-night", Skycons.CLEAR_NIGHT);
  break;

  case "rain":
  $("canvas").attr("id", "rain");
  icons.set("rain", Skycons.RAIN);
  break;

  case "snow":
  $("canvas").attr("id", "snow");
  icons.set("snow", Skycons.SNOW);
  break;

  case "sleet":
  $("canvas").attr("id", "sleet");
  icons.set("sleet", Skycons.SLEET);
  break;

  case "wind":
  $("canvas").attr("id", "wind");
  icons.set("wind", Skycons.WIND);
  break;

  case "fog":
  $("canvas").attr("id", "fog");
  icons.set("fog", Skycons.FOG);
  break;  

  case "cloudy":
  $("canvas").attr("id", "cloudy");
  icons.set("cloudy", Skycons.CLOUDY);
  break;  

  case "partly-cloudy-day":
  $("canvas").attr("id", "partly-cloudy-day");
  icons.set("partly-cloudy-day", Skycons.PARTLY_CLOUDY_DAY);
  break;

  case "partly-cloudy-night":
  $("canvas").attr("id", "partly-cloudy-night");
  icons.set("partly-cloudy-night", Skycons.PARTLY_CLOUDY_NIGHT);
  break;

}

icons.play();

I could settle with this and consider this project done, but I really wanted to find out why I can't access this value outside the original function.

  • 1 helpful hint add `console.log('Setting Value')` directly after `window.little_icon = json.currently.icon;` and `console.log('Accessed Value')` immediately after your switch. You'll notice you are accessing the value before it's being set. Quick fix: wrap the switch in a function and call that function at the end of your getJSON function – wickdninja Dec 30 '16 at 22:07
  • I think I understand what's happening. My switch is executed before the $.getJSON finishes, so the window.little_object was not assigned to its value yet. Excelent answer that one over at the duplicate. It's just that I really didn't know how to search for this question, so I didn't know it was an asynchronous call. – Thiago Rodrigues Dec 30 '16 at 22:09
  • Thank you wickdninja! I'll do that. – Thiago Rodrigues Dec 30 '16 at 22:10
  • awesome you got it – wickdninja Dec 30 '16 at 22:10

0 Answers0