2011-08-29 73 views
0

我有这个PHP:有没有写这个PHP更有效的方式/ jQuery的

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

似乎有很多重复的代码,因此ISN非常干燥的编程。返回的JSON实际上具有与BBC和Sky Sports相同的结构,因此必须有更高效的写作方式。

感谢

+0

当你做'file_get_contents('http:// some_url');',如果远程服务器需要时间来响应,它会冻结你的洞页。我认为为这项任务做一批是个好主意。 –

+0

这是一个AJAX网络应用程序,所以如果PHP页面挂起,用户不会注意到。 – benhowdle89

回答

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

只是为了让事情变得更清洁,你可以这样做:

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

但是,就效率而言,我不认为这是不同的。但是,这至少是更清洁的。

0

只需注意一点,请尝试对DOM进行连续更少的更改。在一些答案中,它是最小的,所以没有太多的问题。但是,与构建空白对象并将它们插入或者对列表等进行大量更改相比,对它们的每个更改都有相当大的开销。

相关问题