Thursday, July 9, 2009

AJAX in FB + setTimeout for auto refresh

this is just a simple example of how this JS function, awaits for data coming from PHP script called chat.php and put everything that gets printed there into a div tag with id 'chat':

function chat_refresh() {
var refresh = 1;
var ajax = new Ajax();
ajax.responseType = Ajax.FBML;
ajax.ondone = function(data) {
document.getElementById('chat').setInnerFBML(data);
}
ajax.requireLogin = 1;
var params = {'refresh' : refresh};
ajax.post("chat.php?refresh", params);
setTimeout(chat_refresh, 1000*10);
}

lets say in chat.php?refresh

you print "hello world";

then "hello world" will get printed in between
before:

after:
hello world
tags in the HTML page.

note:
setTimeout(chat_refresh, 1000*10);
is a function that will automatically call chat_refresh every 10 seconds.

No comments:

Post a Comment