开发者

jQuery inArray spurious results

I have this code:

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

Now, the first time i run this, it always logs 'hello', so it always falls in the first IF statement. Has anyone any idea why?

Here is the relevant PHP as well:

<?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_loa开发者_运维知识库d_string($data1);

$master[0] = $xml;
$master[1] = $xml1;

echo json_encode($master);

?>


Try checking explicitly for -1

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜