Twitter widget callback

How do I call a callback when a Twitter widget is displayed?

Using the code they provided:

TWTR.Widget({data: something}).render().setUser('me').start(); 

Twitter has a deliberately spotty service and frequent long loads. How to add a callback when loading a TWTR widget to show my loader to my users?

+6
javascript twitter
source share
4 answers

I had to track down the author of the library on Twitter, but there is a ready callback : For example.

 new TWTR.Widget({ id: 'twitter-feed', version: 2, . . . features: { scrollbar: false, . . }, ready: function() { jQuery("div#twitter-load").remove(); } }).render().setUser('me').start(); 
+11
source share

The widget has the _rendered property.

I made an example on jsfiddle . Please note that there is no callback, you should poll it to see if it has been completed. In addition, you must assign it to a variable when you create it so that you can access the _rendered property.

I found this with a jsbeautifying script, so it cannot be 100% reliable and definitely not supported.

+1
source share

Using the ash function detection "ready", I needed to add some kind of poll so that I would only delete the download message if any downloadable tweets are loading. To verify this, I will add the following to my hosts file to simulate an error receiving tweets:

 127.0.0.1 api.twitter.com 

Please note that my survey is performed 10 times with an interval of 200 ms. This is because I don't want the code to poll indefinitely when the tweets seem to load in the first 2 seconds (if they download at all). You can adapt these values ​​to suit your situation.

 function pollForTweets() { if (jQuery("div#twitter-feed").find("div.twtr-tweet").length > 0) { jQuery("div#twitter-load").remove(); return; } pollForTweets.pollCounter = pollForTweets.pollCounter || 0; if (pollForTweets.pollCounter < 10) { pollForTweets.pollCounter++; setTimeout('pollForTweets()', 200); } } new TWTR.Widget({ id: 'twitter-feed', version: 2, . . . features: { scrollbar: false, loop: false, live: false, behavior: 'all' }, ready: function () { pollForTweets(); } }).render().setUser('twitter').start(); 
+1
source share

In addition to the return () properties of callback and _rendered (thanks to jasie and harpyon), there are other useful, undocumented properties. For example. results (an array, thus result.length), and showed Results (another array, a subset of the result).

0
source share

All Articles