Google Feed API 的 setTimeout 问题

发布于 2024-11-28 00:29:19 字数 2122 浏览 1 评论 0原文

我使用 google feed API 来读取 Rss feed url 并显示标题。当我直接调用函数 get_rss1_feeds 时它工作正常。但是当我用 setTimeout 或 setInterval 调用它时,我只能看到空白屏幕,并且页面不会停止加载!

<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"></script>
<script type="text/javascript" src="notification.js"></script>

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

我的 jsRss.js 文件

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

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"></script>
<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);             
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-12-05 00:29:19

您需要在 getRss() 函数中设置一个断点,并查看从 setTimeout() 调用它时发生了什么。我的猜测是,该函数中的某些内容存在作用域问题,并且在 setTimeout 运行的全局作用域中不可用,但在您尝试使用它的正常作用域中可用。它可能是变量,也可能是函数不可用。

如果函数是在另一个函数内部声明的,因此实际上在全局范围内不可用,有时会发生这种情况。

仅供参考,这段代码非常奇怪:

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

您可以将其替换为:

var Rsstitle = entry_title;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文