代码之家  ›  专栏  ›  技术社区  ›  Nelga

优化javascript中的函数

  •  5
  • Nelga  · 技术社区  · 14 年前

    我对javascript还比较陌生,但是我已经成功地编写了一个可工作的XML函数:)

    我希望有人能给我一份关于如何优化功能的概要。目前,每个州的天气都有不同的功能,但我希望我能以某种方式简化这个功能。

    代码粘贴在此处: http://pastie.org/private/ffuvwgbeenhyo07vqkkcsw

    非常感谢您的帮助。 谢谢您!

    编辑:添加两个XML源的代码示例:

    功能1(紫外线): http://pastie.org/private/jc9oxkexypn0cw5yaskiq

    功能2(天气): http://pastie.org/private/pnckz4k4yabgvtdbsjvvrq

    5 回复  |  直到 14 年前
        1
  •  1
  •   Gabriele Petrioli    14 年前

    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'},
         ...
     ]
    

    // 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));
    
            });
            });
                });
    }
    
        2
  •  7
  •   bobince    14 年前
    $(d).find('location#sydney')
    

    #sydney sydney ID id="..." getElementById('sydney')

    find() location[id=sydney]

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

    += sydneyuv

    uvindex text() attr() 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;
            }
        });
    });
    

    shorttown

        3
  •  2
  •   Yi Jiang G-Man    14 年前

    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'
    });
    

        4
  •  1
  •   ChaosPandion    14 年前

    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
        };
    
        5
  •  0
  •   Robusto    14 年前