By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,568 Members | 1,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,568 IT Pros & Developers. It's quick & easy.

Problem with Firefox and Ajax batch requests

rizwan6feb
100+
P: 108
Hi experts! Recently i was working on "Form Validation Using Ajax". My form validation was creating problem, when a user changes focus too quickly. I had a post related to this, but was unable to solve the problem. Here is the previous post
http://bytes.com/forum/thread798289.html

Trying to trace the problem I have written a code (Separate from The Form Validation) which sends 300 requests with an interval of 10 miliseconds and displays the output. Now i face a very similar problem. The output is displayed only for the last request. This problem only occurs in Firefox, and the code works fine in IE7.

The code below is a PHP script (counter.php) provides output for ajax request

Expand|Select|Wrap|Line Numbers
  1. <?
  2.     session_start();
  3.     if($_SESSION['count']>0){
  4.         $_SESSION['count']=$_SESSION['count']+1;
  5.     }
  6.     else{
  7.         $_SESSION['count']=1;
  8.     }
  9.     echo $_SESSION['count'];
  10. ?>
  11.  
The code below is HTML to send batch requests

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Ajax Batch Request</title>
  4. <script language="javascript">
  5.     var c=1;
  6.     function sendRequest(){
  7.         a=getAjaxObject();
  8.         a.onreadystatechange=function(){
  9.             if(a.readyState==4){
  10.                 output=a.responseText;
  11.                 var e=document.createElement('b');
  12.                 e.innerHTML='<br>'+output;
  13.                 document.body.appendChild(e);
  14.             }
  15.         }
  16.         a.open("Get","counter.php",true);
  17.         a.send(null);
  18.         if(c<=299){
  19.             setTimeout("sendRequest()",10);
  20.             c++;
  21.         }
  22.     }
  23.     function getAjaxObject(){
  24.         var ajaxRequest;
  25.         try{
  26.             ajaxRequest = new XMLHttpRequest();
  27.         } catch (e){
  28.             try{
  29.                 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  30.             } catch (e) {
  31.                 try{
  32.                     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  33.                 } catch (e){
  34.                     alert("Your browser doesn't support ajax!");
  35.                     return false;
  36.                 }
  37.             }
  38.         }
  39.         return ajaxRequest;
  40.     }
  41.  
  42. </script>
  43. </head>
  44.  
  45. <body>
  46.     <input type="button" value="send request" onclick="sendRequest()" />
  47. </body>
  48. </html>
  49.  
  50.  

If i change the interval on Line # 19 from 10 to 1000, everything works fine
Jun 7 '08 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,390
as you should see ... you loose the references again everytime you start a new request. since you don't use the var keyword you use a global variable and assign a new request before it has processed properly. try to store the references as i suggested the last time.
Jun 7 '08 #2

rizwan6feb
100+
P: 108
Thanks gits! You are right, I was missing the 'var' keyword on line# 7. You gave me the idea of arrays in the last post, which i was unable to implement in my code. But this time i hope there will be no problem
Jun 7 '08 #3

gits
Expert Mod 5K+
P: 5,390
please show your attempt and tell where you have problems ...

kind regards
Jun 7 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.