Question

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;
}
Was it helpful?

Solution

Try to terminate requests manualy:

var connection;
function longpoll() {
   if(connection != undefined) {
      connection.abort();
   }

   connection = $.ajax({ 
       ...
       complete: function() {
          longpool();
       }
   });
}

It may also be a Firefox/firebug issue (showing aborted connections as running), test it in Chrome.

UPDATE:

"In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period." http://api.jquery.com/jQuery.ajax/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top