views:

59

answers:

3

I have this code

function check(){

$.get("friend.php", { id: friendid }, function(data){

//Do stuff with 'data' variable

});

setTimeout(check,5000);

}

It checks friend.php every 5 seconds and returns the result to the data variable.

What I'm trying to do works just fine in other browsers, but in IE8, after the first time it checks friend.php, the contents of the data variable doesn't change. Actually I don't even think IE8 checks again. It does execute all the other code though, as if it had done it. Although it keeps the "data" var exactly the same.

Am I doing something wrong or is it IE's fault? How can I fix this?

+3  A: 

IE caches the results of AJAX requests. You need to add a dummy variable that always changes to the url. For example:

function check(){

$.get("friend.php", { id: friendid, nonce: Math.floor(Math.random()*10000) }, function(data){

//Do stuff with 'data' variable

});

setTimeout(check,5000);

}
Marius
I prefer `(+new Date())` to add a timestamp in ms. It's shorter and no chance of the same random number being picked (hey, it could happen!) ;-).
Andy E
A: 

may be its a caching issue. You can use cache: false in your ajax request.

See jQuery.ajax( settings )

rahul
A: 

Try to put beforehand:

$.ajaxSetup({
  cache: false,
});
Artem Barger