4

I must admit, this is my first post on this site, so I apologise in advice if I do something wrong (formatting etc).

Anyway, I'm creating a kind of mmo using javascript (and jQuery), and so far everything is running fine in Chrome, Safari, Firefox, etc. However, I've found that somewhere along the line, Internet Explorer crashes.

By reproducing the crash I've narrowed it down to this code:

function getUpdates(){
var data={uid:playerName,area:1,mid:lastMessage};
$.ajax({ 
    url: "getUpdates.py", 
    timeout: 32000,
    data: data,
    type:"GET",
    complete: function(obj, textStatus){
            //handleUpdates(obj);
        getUpdates();
        }
    });
}

Which is supposed to poll for updates over a long time. However, in IE after one reply this code gets stuck in an infinite loop, which will crash the browser. It doesn't seem to crash after every reply, only if there is no server response.

Note, the line that says "complete:..." has been tried as:

success: function(...){getUpdates();...},
error: function(...){getUpdates();...}

with the same problem occurring.

Sylvan
  • 43
  • 1
  • 3
  • 1
    It looks like you did everything right... congratulations! However, you should probably add `javascript` and/or `ajax` tags instead of `long-polling`. – SLaks Sep 06 '10 at 02:27
  • Why not put a timeout before calling getUpdates() again. – James Black Sep 06 '10 at 02:28
  • the server takes a long time to respond to requests, (that's why there is a 32 second timeout limit) some requests may take up to 30 seconds before a reply is generated. The game is real-time, so adding extra client-side delay in between gets is very noticeable, and doesn't save much bandwidth. – Sylvan Sep 06 '10 at 02:53

1 Answers1

12

IE is returning the AJAX call instantly from a cache.

You should add a random parameter to the URL to force IE to ignore its cache, like this:

url: "getUpdates.py?RandomNumber=" + Math.random(), 

(You can also use new Date)


Also, you should probably check for updates a little more slowly by adding a 5 second delay:

complete: function(obj, textStatus){
        //handleUpdates(obj);
    setTimeout(function() { getUpdates(); }, 5000);   //milliseconds
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    +1 - I didn't consider that it was cached, so turning off the caching may resolve this. I just assume caching is off for ajax calls, bad assumption. – James Black Sep 06 '10 at 02:31
  • 1
    thanks, I tried this and it worked like a charm! I didn't realise browsers would cache AJAX calls, thanks. – Sylvan Sep 06 '10 at 02:51
  • 1
    You should be able to add the setting `cache: false` to the ajax function and it'll do the job of adding a random (well, timestamp) parameter to the URL for you – andyface May 08 '14 at 12:16
  • 1
    Related: http://stackoverflow.com/questions/4303829/how-to-prevent-a-jquery-ajax-request-from-caching-in-internet-explorer – StvnW Nov 11 '14 at 19:46