2011-08-27 80 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