Darius
Darius

Reputation: 1664

JS - Getting alert multiple times while long polling on success?

I was following a video tutorial on how to do long polling, and it seems I got it done, but I'm having one issue, for some reason, though I get one response from server, my alert shows up 3-6 times. I thought "success" only happens once if we got one response.

2nd question is, how can I make this javascript code cancel the ajax call every 30 seconds, and restart it? I've put in setInterval with .abort() in there while experimenting with no luck, probably wrong placement.

Thank you for the wisdom and help!

var timestamp = null;
var imp = null;
var annk = null;
function waitForMsg(){
    $.ajax( 
    {  
        type: "GET",  
        url: "/test?timestamp=" + timestamp + "&imp=" +imp + "&annk=" +annk,
        dataType : 'json',  
        async: true,
        cache: false,
        success: function(data) 
        {
                            alert("hello");
                if(data.annkr > "0"){
                    $("#myidone").css("background-color", "#cccccc");
                }else{
                    $("#myidone").css("background-color", "#cccccc");
                }
                if(data.impr > 0){
                    $("#myidtwo").css("background-color", "#000000");
                }else{
                    $("#myidtwo").css("background-color", "#000000");
                }
            annk = data.annkr;
            imp = data.impr;
            timestamp = data.timestamp;
            setTimeout('waitForMsg()',2000);    
        }
    });
}   

$(document).ready(function(){
    waitForMsg();
});

I read stuff on Stackoverflow about readystates, but how do I make sure it's ready only once and does it after it's ready?

Upvotes: 0

Views: 147

Answers (3)

Roger Barreto
Roger Barreto

Reputation: 2284

Actually you should use setInterval instead of setTimeout, using this approach if your ajax call fails, you will forcelly abort the loop.

You could also, cancel or change the timeout timings depending on the usage of your service for network resource sanity.

Upvotes: 0

Evan Shortiss
Evan Shortiss

Reputation: 1658

You might be seeing the alert multiple times due to the setTimeout() in your success function, you keep calling the waitForMsg() function. Just a guess.

To abort the request you could do something like this:

var timer = null;
function waitForMessage() {
  var req = $.ajax(YOUR_CODE);

  // The 30 second timeout
  timer = setTimeout(function() {
    req.abort();
    waitForMessage();
  }, 30000);
}

Or slightly better maybe:

function waitForMsg() {
  $.ajax({
    timeout: 30000,
    error: function(err) {
      if(err === 'timeout') {
        waitForMsg();
      }
    }
  })
}

Upvotes: 0

Paolo Casciello
Paolo Casciello

Reputation: 8202

If the code is exactly the one you posted, there's no reason to show the alert more than 1 and then after 2s for the rescheduling.

Can you make a jsFiddle for that showing the problem?

The second question is more interesting. You can use the timeout option in the .ajax call and then, in the error handler, just reschedule the call.

My personal suggestion is to refactor your code to use the new JQuery Ajax style base on .done .fail and .always.

And that setTimeout should be written as setTimeout(waitForMsg,2000). Using the string parameter you evaluate that string instead of just calling the function and it's a performance penalty (so small that's hardly noticeable but that is ).

Upvotes: 1

Related Questions