Optimize Function in Javascript

I am new to javascript, but I managed to write a working xml function :)

I was hoping someone could tell me how to optimize the function. Currently, there is a different function for each state weather, but I was hoping that I could somehow simplify it.

The code is pasted here: http://pastie.org/private/ffuvwgbeenhyo07vqkkcsw

Any help is appreciated. Thank!

EDIT: EXAMPLES OF EXAMPLES OF EXAMPLES OF REVERSE XML-FEEDS:

Function 1 (UV): http://pastie.org/private/jc9oxkexypn0cw5yaskiq

Function 2 (weather): http://pastie.org/private/pnckz4k4yabgvtdbsjvvrq

+5
source share
5

, UV-UV, (stationid)

var areas = [
     {id:'sydney',suffix:'syd',stationid:'YSSY'},
     {id:'melbourne',suffix:'mel',stationid:'YMML'},
     {id:'brisbane',suffix:'bri',stationid:'YBBN'},
     {id:'perth',suffix:'per',stationid:'YPPH'},
     ...
 ]

UV

// FUNCTION 1 - UV
function getUV() {

    // BEGIN AUSTRALIAN UV FUNCTION

    $.get('http://www.arpansa.gov.au/uvindex/realtime/xml/uvvalues.xml', function(d) {

        //SYDNEY UV
             $(areas).each(function(){
            var area = this;
        $(d).find('location#'+area.name).each(function(){

            var $location = $(this); 
            var uvindex = $location.find('index').text();

            var areauv = areauv += '<span>' + uvindex + '</span>' ;
            $('#uv-'+area.suffix).empty().append($(areauv)); // empty div first

            if (uvindex <= 2.0) {
                $('#risk-'+area.suffix).empty().append('Low');
                $('#curcon-'+area.suffix).empty().append('You can safely stay outdoors and use an SPF 15 moisturiser.');
            } else if (uvindex <= 5.0) {
                $('#risk-'+area.suffix).empty().append('Moderate');
                $('#curcon-'+area.suffix).empty().append('Wear protective clothing outdoors and use an SPF 15 or SPF 30 moisturiser.');
            } else if (uvindex <= 7.0) {
                $('#risk-'+area.suffix).empty().append('High');
                $('#curcon-'+area.suffix).empty().append('Wear protective clothing, limit your time outdoors and use an SPF 30 moisturiser.');
            } else if (uvindex <= 10.0) {
                $('#risk-'+area.suffix).empty().append('Very High');
                $('#curcon-'+area.suffix).empty().append('Use caution, limit exposure to the sun and use an SPF 30 moisturiser.');
            } else if (uvindex <= 20.0) {
                $('#risk-'+area.suffix).empty().append('Extreme');
                $('#curcon-'+area.suffix).empty().append('Use extreme caution, avoid exposure to the sun and use an SPF 30 moisturiser.');
            } else {
                $('#risk-'+area.suffix).empty().append('Unavailable');
                $('#curcon-'+area.suffix).empty().append('Information is currently unavailable.');
            }

        });
            });

        // END OF AUSTRALIAN UV FUNCTION

    });
}

function getWeather() {

        // BEGIN AUSTRALIA and NEW ZEALAND WEATHER FUNCTION
            $(areas).each(function(){
                var area = this;
        $.get('http://api.wxbug.net/getLiveCompactWeatherRSS.aspx?ACode=XXXPRIVATEXXX&stationid='+area.stationid+'&unittype=1&outputtype=1', function(d){
        $(d).find('weather').each(function(){

            var $weatherinfo = $(this); 
            var degrees = $weatherinfo.find('temp').text().replace(/\.0$/i, "");
            var conditions = $weatherinfo.find('current-condition').text();
            var icon = $weatherinfo.find('current-condition').attr('icon').replace(/\.gif$/i, ".png").split('http://deskwx.weatherbug.com/')[1];

            var temperature = temperature += '<span>' + degrees + '</span>' ;   
            $('#temp-'+area.suffix).empty().append($(temperature));

            var winformation = winformation += '<span>' + conditions + '</span>' ;
            $('#info-'+area.suffix).empty().append($(winformation));

            var wicon = wicon += '<img src="' + icon + '" alt="Weather Unavailable" />' ;
            $('#icon-'+area.suffix).empty().append($(wicon));

        });
        });
            });
}
+1
$(d).find('location#sydney')

. #sydney value sydney, ID. HTML id="..." ID DOCTYPE. XML DOCTYPE, id="..." ID. , getElementById('sydney') , #sydney .

, , find(), jQuery "Sizzle JavaScript selector matcher", id="...", HTML. Sizzle , . location[id=sydney] XML-.

var sydneyuv = sydneyuv += '<span>' + uvindex + '</span>' ;

. +=, - sydneyuv, sydneyuv.

, HTML . , uvindex HTML? (, , , , , .) HTML- HTML XSS. DOM, text() attr() jQuery, : var $sydneyuv= $('<span/>', {text: uvindex});, .

, - .

. :

var towns= ['sydney', 'melbourne', 'brisbane', 'perth', 'adelaide', 'darwin'];
var uvlevels= [
    {uvlevel: 2,    risk: 'Low',         curcon: 'You can safely stay outdoors and use an SPF 15 moisturiser.'},
    {uvlevel: 5,    risk: 'Moderate',    curcon: 'Wear protective clothing outdoors and use an SPF 15 or SPF 30 moisturiser.'},
    {uvlevel: 7,    risk: 'High',        curcon: 'Wear protective clothing, limit your time outdoors and use an SPF 30 moisturiser.'},
    {uvlevel: 10,   risk: 'Very high',   curcon: 'Use caution, limit exposure to the sun and use an SPF 30 moisturiser.'},
    {uvlevel: 20,   risk: 'Extreme',     curcon: 'Use extreme caution, avoid exposure to the sun and use an SPF 30 moisturiser.'},
    {uvlevel: null, risk: 'Unavailable', curcon: 'Information is currently unavailable.'}
];

:

$.each(towns, function() {
    var $location= $(d).find('location[id='+this+']');
    var uv= $location.find('index').text();
    var shorttown= this.slice(0, 3);
    $('#uv-'+shortttown).empty().append($('<span/>', {text: uv}));
    $.each(uvlevels, function() {
        if (this.uvlevel===null || uv<=this.uvlevel) {
            $('#risk-'+shorttown).text(this.risk);
            $('#curcon-'+shorttown).text(this.curcon);
            return false;
        }
    });
});

, -, .

( HTML, shorttown).

+7

:

var cities = ['sydney', 'melbourne', 'brisbane', 'perth', 'adelaide', 'darwin'], 
    risks = {
        2: {
            risk: 'Low', 
            curcon: 'You can safely stay outdoors and use an SPF 15 moisturiser.'
        }
        5: {
            risk: 'Moderate', 
            curcon: 'Wear protective clothing outdoors and use an SPF 15 or SPF 30 moisturiser.'
        }
        7: {
            risk: 'High', 
            curcon: 'Use caution, limit exposure to the sun and use an SPF 30 moisturiser.'
        }
        10: {
            risk: 'Very High', 
            curcon: 'Use caution, limit exposure to the sun and use an SPF 30 moisturiser.'
        }
        20: {
            risk: 'Extreme', 
            curcon: 'Use extreme caution, avoid exposure to the sun and use an SPF 30 moisturiser.'
        }
    };

for(var i = 0; i < cities.length; i++){
    var shortCityName = cities[i].substring(0, 3);

    $(d).find('location#sydney').each(function(){
        var $location = $(this); 
        var uvindex = parseInt($location.find('index').text(), 10);

        $('#uv-' + shortCityName).html('<span>' + uvindex + '</span>');

        for(var i in risks){
            if(uvindex < risks[i]){
                $('#risk-' + shortCityName).html(risks[i].risk);
                $('#curcon-' + shortCityName).html(risks[i].curcon);
            }
        }
    });
}

, . , :

var wicon = wicon += '<img src="' + icon + '" alt="Weather Unavailable" />' ;

:

var wicon = $('<img /').attr({
    src: icon, 
    alt: 'Weather Unavailable'
});

, , XML- . , API JSONP. JSON () JavaScript.

+2

, .

var lte2 = { risk: "Low", curcon: "You can safely stay outdoors and use an SPF 15 moisturiser." },
    lte5 = { risk: "Moderate", curcon: "Wear protective clothing outdoors and use an SPF 15 or SPF 30 moisturiser." },
    lte7 = { risk: "High", curcon: "Wear protective clothing, limit your time outdoors and use an SPF 30 moisturiser." },
    uvWarningMap = {
        0: lte2,
        1: lte2,
        2: lte2,
        3: lte5,
        4: lte5,
        5: lte5,
        6: lte7,
        7: lte7
    };
+1

, . , , . .

0

All Articles