2011-08-27 81 views
1

我有這樣的代碼:jQuery的inArray虛假結果

var myRSS =[]; 

function rssReader() { 

    $.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)) { 
       //do nothing 
       console.log('hello'); 
      } else { 
       // save item title in the array 
       myRSS.push(item.title); 
       console.log(myRSS); 

       // publish item 
       $('.container').append("<a href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>"); 
      } 
     }); 

    }); 

} 

setInterval(rssReader(), 5000); 

現在,我第一次運行它,它會隨時記錄「你好」,所以它總是落在第一個IF語句。有沒有人知道爲什麼?

下面是相關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); 

?> 

回答

3

嘗試爲-1

if (jQuery.inArray(item.title, myRSS) != -1) { 
+0

我只是工作了這一點,因爲你回答,謝謝明確檢查! – benhowdle89