tags:

views:

59

answers:

2

I have a cross-domain long polling request using getJSON with a callback that looks something like this:

$.getJSON("http://long-polling.com/some.fcgi?jsoncallback=?"
    function(data){
          if(data.items[0].rval == 1) {
            // update data in page
          }
    });

The problem is the request to the long-polling service might take a while to return, and another getJSON request might be made in the meantime and update the page even though it is a stale response.

Req1: h**p://long-polling.com/some.fcgi at 10:00 AM

Req2: h**p://long-polling.com/some.fcgi? at 10:01 AM

Req1 returns and updates the data on the page 10:02 AM

I want a way to invalidate the return from Req1 if Req2 has already been made.

Thanks!

+2  A: 

When you make the ajax request it returns the XMLHttpRequest object. If you want to abort the call you can call the .abort() method on it.

var request = $.getJSON(. . . . . );
request.abort();
PetersenDidIt
+1. Was about to give just the same answer.
Boldewyn
A: 

I don't think that works as the getJSON uses JSONP method which actually works by dynamically inserting a script tag which your browser executes. I don't think there is any way to stop the browser from executing the script...is there?

NewToDB