开发者

Is there a more efficient way to write this PHP/jQuery

I have this PHP:

<?php

$data = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml');

$xml = simplexml_load_string($data);

$data1 = file_get_contents('http://www.skysports.com/rss/0,20514,11661,00.xml');

$xml1 = simplexml_load_string($data1);

$master[0] = $xml;
$master[1] = $xml1;

echo json_encode($master);

?>

And this jQuery:

    var myRSS =[];

function rssReader() {

    console.log('ran');

    $.getJSON('bbc.php', function(data){
        console.log(data);
        $.each(data[0].channel.item, function(index, item){
            // check if item title is stored in the array   
            if (jQuery.inArray(item.title, myRSS) != -1) { 
                //do nothing
            } else {
                // save item title in the array
                myRSS.push(item.title);

                // publish item  
                $('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
                $("title").text('EMG: ' + item.title);
                $('#loader').remove();
            }
        });

        $.each(data[1].channel.item, function(index, item){
            // check if item title is stored in the array   
            if (jQuery.inArray(item.t开发者_开发知识库itle, myRSS) != -1) { 
                //do nothing
            } else {
                // save item title in the array
                myRSS.push(item.title);

                // publish item  
                $('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
                $("title").text('EMG: ' + item.title);
            }
        });

    });

}

rssReader();

setInterval(rssReader, 10000);

There seems to be a lot of repeated code and therefore isn't very DRY programming. The returned JSON actually has the same structure, from BBC and Sky Sports so there must be a more efficient way of writing this.

Thanks


you can cut your jquery down to this(havent tested it there might be a typo):

  var myRSS =[];

    function rssReader() {

        console.log('ran');

        $.getJSON('bbc.php', function(data){
            console.log(data);
            $.each(data[0].channel.item, function(index, item){
                linkhandler(item)
                if (jQuery.inArray(item.title, myRSS) == -1) { 
                    $('#loader').remove();
                }
            });

            $.each(data[1].channel.item, function(index, item){
               linkhandler(item)
            });

        });

    }
    function linkhandler(item)
    { // check if item title is stored in the array   
       if (jQuery.inArray(item.title, myRSS) == -1) { 
             // save item title in the array
             myRSS.push(item.title);
             $('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
             $("title").text('EMG: ' + item.title);
       }
    }
    rssReader();

    setInterval(rssReader, 10000);


Just to make things a little cleaner, you could do something like this:

var myRSS =[];

var handleData = function(index, item) {
    // check if item title is stored in the array   
    if (jQuery.inArray(item.title, myRSS) != -1) { 
        //do nothing
    } else {
        // save item title in the array
        myRSS.push(item.title);

        // publish item  
        $('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
        $("title").text('EMG: ' + item.title);
        $('#loader').remove();
    }
}


function rssReader() {
    console.log('ran');

    $.getJSON('bbc.php', function(data){
        console.log(data);
        $.each(data[0].channel.item, handleData);
        $.each(data[1].channel.item, handleData);
    });

}

rssReader();

setInterval(rssReader, 10000);

But, as far as efficiency is concerned, I don't think it's that different. But, it's at least cleaner.


Just a note to be aware of, try and make less consecutive changes to the DOM. In some of the answers it is minimal anyway so not too much of a problem. But each change to them DOM has a massive overhead compared to say constructing blank objects and inserting them en mass or making mass changes to lists and the like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜