setTimeout problem with Google Feed API

5,455 views
Skip to first unread message

yesh

unread,
Aug 5, 2011, 12:19:56 AM8/5/11
to Google AJAX APIs
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);
}

I did just as you said. console.log at the start of both the functions
are being outputted (I have documented it inside the code). This is
the entire code I am working on. I am planing to use more than one Rss
feed so I will be creating similar functions like get_rss2_feeds etc
and each of them will have there own setTimeout method which will be
calling them at unique time intervals.

I did debug it using the breakpoint in my chrome as you said.I set the
breakpoint on my getrss function the page is being displayed when the
breakpoint move to google.setOnLoadCallback(Load); the page goes
blank. Then it comes out of getRss function and comes out of
get_rss1_feeds() function. –

Could someone help me solve this problem.

Jeremy Geerdes

unread,
Aug 5, 2011, 9:38:51 AM8/5/11
to google-ajax...@googlegroups.com
The problem is that, when you use the setTimeout, the page is finishing its load before your get_rss1_feeds function is called. More specifically, you call google.load('feeds','1') in getRSS. As it is presently called, the google.load call uses document.write to add the appropriate libraries. Using document.write after the page has finished its initial load is causing the blank page that you're seeing.

To fix the issue, you have a number of options. Probably the simplest would be to move the google.load(...); call(s) outside of the functions so that it's called - and the libraries are loaded - as the page loads. Alternatively, you could use it like this: google.load('feeds', '1', {callback : function(){DO_SOMETHING}}); This usage would compel the loader to dynamically create and append the script to the page rather than using the document.write method. The result would be that your page would load normally, and the API could be loaded at any time after that without ill effects. The challenge is that you'll have to rewrite most of your script so that anything that relates to the Feeds API, etc., is saved until the callback function.

Jeremy R. Geerdes
Generally Cool Guy
Des Moines, IA

For more information or a project quote:
jrge...@gmail.com

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

> --
> You received this message because you are subscribed to the Google
> Groups "Google AJAX APIs" group.
> To post to this group, send email to
> google-ajax...@googlegroups.com
> To unsubscribe from this group, send email to
> google-ajax-searc...@googlegroups.com
> To view this message on the web, visit
> http://groups.google.com/group/google-ajax-search-api?hl=en_US
> For more options, visit this group at
> http://groups.google.com/group/google-ajax-search-api?hl=en?hl=en

Diogo Garcia

unread,
Jul 23, 2014, 12:59:28 PM7/23/14
to google-ajax...@googlegroups.com
Your solution worked perfectly!

I'm now using for my translator:
 google.load('language', '1', {callback : function(){
 init
();
 
}});

Instead of:
google.load("language", "1");
google.setOnLoadCallback(init);

Thanks!

Diogo Garcia

unread,
Jul 23, 2014, 1:01:09 PM7/23/14
to google-ajax...@googlegroups.com, callm...@gmail.com
Thanks!

That worked great for my translator:
Used to be:
    google.load("language", "1");
    google
.setOnLoadCallback(init);



Now It is:
 google.load('language', '1', {callback : function(){
 init
();
 
}});


Thanks!
Reply all
Reply to author
Forward
0 new messages