Friday, April 8, 2011

Wtf IE7 - AJAX calls using setTimeout

Hi

I have tested this on Firefox, Opera and Seamonkey. It works fine. When it comes to Internet Explorer 7. It works but upto a certain point. I am making an AJAX call to a PHP script every few seconds. In IE7 it makes the first AJAX call and it retrieves the data but it doesn't do it again ever. Even though i have a setTimeout function in the else block. WHY? :(

startTime = setTimeout('getStatus()', 5000); 

}//function convertNow

function getStatus() {

 $.ajax({
 type: "GET",
 url: "fileReader.php",
 data: 'textFile=' + fileNameTxt,
 success: function(respomse){
 textFileResponse = respomse.split(" ");
 $("#done").html("Downloading & Converting Video...<b style='font-size:17px;color:green;'>" + textFileResponse[0] + "</b><br /><b>" + properFileName + '</b>');
 }
 });//ajax

 if(textFileResponse[0]=='100.0%'){

 }
 else{  
 continueTime = setTimeout('getStatus();', 3000); 
  alert('call end');
 }

}

Apologies if any frustration comes through this question. I've been running around like a headless chicken for the past 3 hours.

Thank you for any help.

EDIT 2

I have added the full function. The setTimeout seems to be working correctly. It must be the AJAX call, am just checking what is being returned. Even stranger! It keeps returning the same value from the AJAX request and its not getting any newer values!! I think Answer 2 might have something.It may be due with cache but how do you over come that?

From stackoverflow
  • not at all sure on this but are you missing the ';'?

    setTimeout('getStatus()', 3000);

    -> setTimeout('getStatus();', 3000);

    Abs : I just tried that and it didn't help IE7 make the call again!
    bobince : Incidentally you can save yourself a pointless string compilation by saying “setTimeout(getStatus, 3000);” directly on the getStatus function object.
  • Are you requesting the ajax call via HTTP GET as opposed to HTTP POST? IE tends to use cached results of ajax calls unless you use POST instead of GET.

    EDIT: Since you've updated your question, I can see that you are indeed using the GET verb. Change it to POST and I bet your issue will be resolved.

    Abs : I am using GET, but after every test and change I make I clear the cache and reload the page, and still gives me the same output!
    Chetan Sastry : @Abs: What Ken meant was that IE caches results between subsequent AJAX calls. Clearing the cache before reloading page doesnt help. You must explicitly set a header telling IE not to cache.
    Abs : I am understanding now and he is right i think (see edit 2). How can I do this?
    Abs : Ah, use POSt instead of GET!
    Abs : You are a freaking legend!!
    bobince : Or include ‘cachebuster’ random-number-parameters.
  • I noticed that textFileResponse is set in the function declaration of the success handler for the AJAX call yet referenced as an array immediately after making the AJAX call. Perhaps in IE 7 the success handler hasn't completed before the array reference which would throw a java script error, thus the setTimeout would never run.

0 comments:

Post a Comment