I'm making a chat which is based on long polling (something like this )with PHP and jQuery. once whole page is downloaded in browser, a function makes a long polling request to the back-end with some timeout limit, when data comes from back-end it again makes the long-polling request and if any error, it will again make new long-polling request.
Problem : analyzing the traces by firebug, I've noticed that some times the long polling request is running 3 or 4 times, however it should not. there should only one long-polling request running per page.
however the code works perfectly. but long-polling request duplication is the issue.
function listen_for_message(){
// this functions is makes the long-polling request
$.ajax({
url: "listen.php",
timeout:5000,
success: function(data) {
$('#display').html(data);
listen_for_message();
}
error: function() {
setTimeOut("listen_for_message()",2000); // if error then call the function after 2 sec
}
});
return;
}