Sunday, 25 August 2013

usage of setTimeout function in AJAX

usage of setTimeout function in AJAX

I.m currently following a tutorial about how to load content from a MYSQL
db without reloading the page.
I just want to understand the use of setTimeout in this code. What's it
for? I tried removing that part and ajax still works. Why would you need
to delay a task, isn't ajax meant to be realtime update?
// load
$(document).ready(function () {
done();
});
// timeout function
function done() {
setTimeout(function () {
updates();
done();
}, 200);
}
// fetch data from db
function updates() {
$.getJSON("update.php", function (data) {
$("ul").empty();
$.each(data.result, function () {
$("ul").append("<li>ID: " + this['msg_id'] + "</li><br
/><li>ID: " + this['msg'] + "</li><br />");
});
});
}

No comments:

Post a Comment