Lo primero que debemos de hacer es la estructura HTML que nos mostrarán los datos.
<h2> Latest Tweets </h2>
<ul id="tweets"> </ul>
Luego de incluir la librería jquery debemos buscar los últimos mensajes en twitter para esto usamos elsiguiente script:
(function() {
var UpdatePanel = {
init : function(options) {
this.options = $.extend({
interval : 5000,
number : 3
}, options);
this.updater();
},
Aquí definimos algunos parámetros como el tiempo de actualización o la cantidad de elementos a mostrar.
updater : function() {
(function updateBox() {
this.timer = setTimeout(function() {
updateIt();
updateBox();
}, UpdatePanel.options.interval);
})();
Lo siguiente que hacemos es definir la función update, la cual a su vez invoca a updateIt y updateBox.
updateIt();
function updateIt() {
$.ajax({
/// tipo de llamada AJAX, url, y tipo JSONP
type : 'GET',
url : UpdatePanel.options.url,
dataType : 'jsonp',
error : function() {},
// Si se consiguen los datos debe actualizar los resultados
success : function(results) {
var theTweets = '',
elem = UpdatePanel.options.elem.empty();
$.each(results.results, function(index, tweet) {
if ( index === UpdatePanel.options.number ) {
return false;
}
else {
// Definimos lo que vamos a mostrar
theTweets += '' + tweet.text + '';
}
});
// Finalmente añadimos los ultimos comentarios
elem.append(theTweets);
}
});
En el siguiente código, invocamos a la función updateIt.
UpdatePanel.init({
interval : 5000,
number : 5,
url : "http://search.twitter.com/search.json?q=batman",
elem : $('#tweets')
});
Finalmente vamos a inicializar nuestro objeto, definiendo los parámetros respectivos además del lugar donde se mostrarán los útimos mensajes
Vía: Miguelmanchego






