cloudy cloudy

Author Topic: Wind direction function cleanup for AJAX pages  (Read 3355 times)

0 Members and 1 Guest are viewing this topic.

Offline TechScribe

  • Posts: 45
  • Davis Weather Vue
  • Kirkland, Washington USA
    • KirklandWeather.com
Wind direction function cleanup for AJAX pages
« on: November 19, 2006, 09:54:18 PM »
If you've gone to an AJAX version of the Carter Lake template so your values dynamically update (thanks, Pinto!), here's a function for generating the wind direction label using an array.

Instead of parsing the wind direction value and assigning text labels here:

//Winddirection ...
      val = x.responseText.split(' ')[3];
                        if (val >= 349 && val <= 360)
                        val = "N"
                        if (val >= 0 && val < 12)
                        val = "N"
                        if (val >=12  && val < 34)
                        val = "NNE"   
                        if (val >=34 && val < 56)
                        val = "NE"
                        if (val >= 56 && val < 79)
                        val = "ENE"
                        if (val >= 79 && val < 101)
                        val = "E"
                        if (val >= 101 && val < 124)
                        val = "ESE"
                        if (val >= 124 && val < 146)
                        val = "SE"
                        if (val >= 146 && val < 169)
                        val = "SSE"
                        if (val >= 169 && val < 191)
                        val = "S"
                        if (val >= 191 && val < 214)
                        val = "SSW"
                        if (val >= 214 && val < 236)
                        val = "SW"
                        if (val >= 236 && val < 259)
                         val = "WSW"
                        if (val >= 259 && val < 281)
                        val = "W"
                        if (val >= 281 && val < 304)
                        val = "WNW"   
                        if (val >= 304 && val < 326)
                        val = "NW"
                        if (val >= 326 && val < 349)
                        val = "NNW" 
      document.getElementById("ajaxwinddir").innerHTML = "&nbsp;" + val;
       document.getElementById("ajaxwindicon").innerHTML = '&nbsp;<img src=./images/winddir/' + val + '.gif width="14" height="14" alt="Wind from ' + val + '" title="Wind from ' + val + '"></img>';


add a function to the top of the AJAX script section:

function windDir ($winddir)
// Take wind direction value, return the
// text label based upon 16 point compass
{
   $windlabel = new Array("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");
   return $windlabel[Math.floor(((parseInt($winddir) + 11) / 22.5) % 16 )];
}


Then replace the wind direction section with:

      //WIND DIRECTION ...
      val = windDir(x.responseText.split(' ')[3]);

      document.getElementById("ajaxwinddir").innerHTML = "&nbsp;" + val;
       document.getElementById("ajaxwindicon").innerHTML = '&nbsp;<img src="./images/winddir/' + val + '.gif" width="14" height="14" alt="Wind from ' + val + '" title="Wind from ' + val + '">';

Offline TechScribe

  • Posts: 45
  • Davis Weather Vue
  • Kirkland, Washington USA
    • KirklandWeather.com
Re: Wind direction function cleanup for AJAX pages
« Reply #1 on: November 19, 2006, 10:07:53 PM »
Well, duh. #-o I see Kevin already figured this out in another post.

Long live arrays!