I\'ve written a little chat-box widget which runs an ajax call every second, to fetch new messages that have been posted. The problem is it\'s leaking memory, and after only
Never use setInterval()
with ajax, otherwise your requests never stay synchronized. Use setTimeout()
instead and then pending your logic, initiate the setTimeout()
recursively in the complete
callback.
Example.
$(DoMyAjax); // start your ajax on DOM ready
function DoMyAjax() {
$.ajax({
complete: function() {
// your logic here
setTimeout(DoMyAjax, 1000);
}
});
}