我有这段代码:
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);
现在,我第一次运行它,它总是记录'hello',所以它总是落在第一个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);
?>
答案 0 :(得分:3)
尝试显式检查-1
if (jQuery.inArray(item.title, myRSS) != -1) {