【问题标题】:Is there a more efficient way to write this PHP/jQuery有没有更有效的方法来编写这个 PHP/jQuery
【发布时间】:2011-08-29 13:18:24
【问题描述】:

我有这个 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);

?>

还有这个 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.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);
            }
        });

    });

}

rssReader();

setInterval(rssReader, 10000);

似乎有很多重复的代码,因此不是很干燥的编程。返回的 JSON 实际上具有相同的结构,来自 BBC 和 Sky Sports,因此必须有一种更有效的方式来编写它。

谢谢

【问题讨论】:

  • 当你file_get_contents('http://some_url');时要小心,如果远程服务器需要时间来响应,它会冻结你的漏洞页面。我认为为这个任务做一个批处理是个好主意。
  • 这是一个 AJAX 网络应用程序,所以如果 PHP 页面挂起,用户不会注意到。

标签: php jquery ajax performance json


【解决方案1】:

你可以把你的 jquery 缩减到这个(没有测试过可能有错别字):

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

【讨论】:

    【解决方案2】:

    为了让事情更简洁,你可以这样做:

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

    但是,就效率而言,我认为没有那么不同。但是,它至少更干净。

    【讨论】:

      【解决方案3】:

      请注意,尽量减少对 DOM 的连续更改。在某些答案中,无论如何它都是最小的,所以不是太大的问题。但是与构建空白对象并大量插入它们或对列表等进行大量更改相比,对它们 DOM 的每次更改都会产生巨大的开销。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-24
        • 1970-01-01
        • 1970-01-01
        • 2012-09-12
        • 1970-01-01
        相关资源
        最近更新 更多