Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Ajax calls crashing Internet Explorer?

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.

like image 281
Sylvan Avatar asked Dec 16 '22 22:12

Sylvan


1 Answers

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
}
like image 154
SLaks Avatar answered Dec 29 '22 11:12

SLaks