Multiple ajax calls on one page
I'm trying to create a chat app with jQuery without the need for setTimeout to minimize the number of ajax requests:
function checkChat(){
new jQuery.ajax({
'url' : './chat/check.php',
'cache' : false,
'success' : function(messages) {
if( messages.length ) {
$("#empty_chat").append(messages);
//write to chat wall
}
checkChat();
}
});
}
On a simple page that works great, but on a page where there are other ajax events (like navigation), the request is queued and nothing is displayed until the messages are empty.
The cursor is also in a waiting state.
Is there a way to solve this?
0
source to share
2 answers
Not really, if you don't want to peek into a comet, you still have to poll for messages, and setTimeout is fine.
The recursion you set where checkChat () is called recursively must be set to setTimeout or you will overflow the lol stream
setTimeout(checkChat,2000);
and you need to remove the new operator and just do
jQuery.ajax or $ .ajax
0
source to share