简体   繁体   中英

Instead of repeating blocks of code, more efficient way to write this javascript?

Here i'm just repeating the same code over and over and just incrementing the variable names. Same with the feed url call to the RSS2JSON utility. Can I somehow write this so I don't have to repeat? I actually have 8 different feeds I'd like to use.

<script type="text/javascript">
        var cpacontent = document.getElementById('cpa');
        function showFeed(data){
            if(data.status == 'ok'){    
                var feedlength = data.items.length;
                var output = '';
                for(var i=0;i<1;++i){
                    output += '<p><a href="' +
                    data.items[i].link + '" target="_blank" >' +
                    data.items[i].title + '</a>';
                }
                cpacontent.innerHTML = output;
            }
        }

        var fscontent = document.getElementById('fs');
        function showFeed2(data){
            if(data.status == 'ok'){    
                var feedlength2 = data.items.length;
                var output2 = '';
                for(var i=0;i<1;++i){
                    output2 += '<p><a href="' +
                    data.items[i].link + '" target="_blank" >' +
                    data.items[i].title + '</a>';
                }
                fscontent.innerHTML = output2;
            }
        }
        var wealthcontent = document.getElementById('wealth');
        function showFeed3(data){
            if(data.status == 'ok'){    
                var feedlength3 = data.items.length;
                var output3 = '';
                for(var i=0;i<1;++i){
                    output3 += '<p><a href="' +
                    data.items[i].link + '" target="_blank" >' +
                    data.items[i].title + '</a>';
                }
                wealthcontent.innerHTML = output3;
            }
        }    
</script>
<script type="text/javascript" src="http://rss2json.com/api.json?callback=showFeed1&rss_url=http%3A%2F%2Ffeedurlplaceholder1"></script>

<script type="text/javascript" src="http://rss2json.com/api.json?callback=showFeed2&rss_url=http%3A%2F%2Ffeedurlplaceholder2"></script>

<script type="text/javascript" src="http://rss2json.com/api.json?callback=showFeed3&rss_url=http%3A%2F%2Ffeedurlplaceholder3"></script>

向函数添加第二个参数以接受元素。

Something like:

<script>
function myFunction(selectorID, data) {
    var element = document.getElementById('selectorID');
    if(data.status == 'ok'){    
        var dataLength = data.items.length;
        var output = '';
        for(var i = 0; i < 1; ++i){
            output += '<p> <a href="' +
            data.items[i].link + '" target="_blank" >' +
            data.items[i].title + '</a>';
        }
        cpacontent.innerHTML = output;
    }
}

myFunction("cpa", cpaData);
myFunction("fs", fsData);
myFunction("wealth", wealthData);
</script>

Hope you doing well..!!

you can use one function showFeed() with two parameters that are data and varFlag as follows:

<script type="text/javascript">
     function showFeed(data, varFlag){
         if(data.status == 'ok'){    
             var feedlength = data.items.length;

             var output = '';
             for(var i=0;i<1;++i){
                 output += '<p><a href="';
                 output += data.items[i].link + '" target="_blank" >';
                 output += data.items[i].title + '</a>';
             }

             if(varFlag == 'cpa') { cpacontent.innerHTML = output; }

             if(varFlag == 'fs') { fscontent.innerHTML = output; }

             if(varFlag == 'wealth') { wealthcontent.innerHTML = output; }
         }
     }

     var cpacontent = document.getElementById('cpa');
     showFeed(data, 'cpa');

     var fscontent = document.getElementById('fs');
     showFeed(data, 'fs');

     var wealthcontent = document.getElementById('wealth');     
     showFeed(data, 'wealth');    
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM