开发者

setTimeout problem with Google Feed API

开发者 https://www.devze.com 2023-03-26 04:34 出处:网络
I have used google feed API to read a Rss feed url and display the title. When I call the function get_rss1_feeds directly It works fine. But when I call it with setTimeout or setInterval I am able to

I have used google feed API to read a Rss feed url and display the title. When I call the function get_rss1_feeds directly It works fine. But when I call it with setTimeout or setInterval I am able to see only blank screen and the page does not stop loading!!

<script src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0" type="text/javascript"></script>     
<script type="text/javascript" src="jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="query.mobile-1.0a4.1.min.js"></script>
<script type="text/javascript" src="jsRss.js"></scrip开发者_开发知识库t>
<script type="text/javascript" src="notification.js"></script>

My notification.js

/** global variable **/
var Rsstitle;
/** end global variable **/

document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
// Empty
}

function get_rss1_feeds() {
    console.log('test');               // this is being outputted
    var Rss1_title = getRss("http://yofreesamples.com/category/free-coupons/feed/?type=rss", function(entry_title) {    
        if(Rsstitle != entry_title)
        Rsstitle = entry_title;
        console.log('test1',Rsstitle); // not working
    });   
}

//get_rss1_feeds() works fine
setTimeout(get_rss1_feeds,5000); 

My jsRss.js file

    function getRss(url, callback){
    console.log('test2');            // this is being outputted 
    if(url == null) return false;
    google.load("feeds", "1");
    // Our callback function, for when a feed is loaded.
    function feedLoaded(result) {
        if (!result.error) {
            var entry = result.feed.entries[0];
            var entry_title = entry.title; // need to get this value
            callback && callback(entry_title);        
        }
    }
    function Load() {
        // Create a feed instance that will grab feed.
        var feed = new google.feeds.Feed(url);
        // Calling load sends the request off.  It requires a callback function.
        feed.load(feedLoaded);      
    }    
    google.setOnLoadCallback(Load);             
}


You need to set a breakpoint in the getRss() function and see what's going on when it's called from setTimeout(). My guess would be that something in that function has a scoping issue and isn't available from the global scope that setTimeout runs in, but is available from the normal scope you tried it in. It could be variables or it could be functions that aren't available.

This can sometimes happen if functions are declared inside another function and thus aren't actually available globally.

FYI, this block of code is very odd:

var Rsstitle;
if(Rsstitle != entry_title)         
Rsstitle = entry_title;

You can replace it with this:

var Rsstitle = entry_title;
0

精彩评论

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