Problem with Firefox and Ajax batch requests

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rizwan6feb
    New Member
    • Jul 2007
    • 108

    Problem with Firefox and Ajax batch requests

    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


    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

    Code:
    <?
    	session_start();
    	if($_SESSION['count']>0){
    		$_SESSION['count']=$_SESSION['count']+1;
    	}
    	else{
    		$_SESSION['count']=1;
    	}
    	echo $_SESSION['count'];
    ?>
    The code below is HTML to send batch requests

    Code:
    <html>
    <head>
    <title>Ajax Batch Request</title>
    <script language="javascript">
    	var c=1;
    	function sendRequest(){
    		a=getAjaxObject();
    		a.onreadystatechange=function(){
    			if(a.readyState==4){
    				output=a.responseText;
    				var e=document.createElement('b');
    				e.innerHTML='<br>'+output;
    				document.body.appendChild(e);
    			}
    		}
    		a.open("Get","counter.php",true);
    		a.send(null);
    		if(c<=299){
    			setTimeout("sendRequest()",10);
    			c++;
    		}
    	}
    	function getAjaxObject(){
    		var ajaxRequest;
    		try{
    			ajaxRequest = new XMLHttpRequest();
    		} catch (e){
    			try{
    				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    			} catch (e) {
    				try{
    					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e){
    					alert("Your browser doesn't support ajax!");
    					return false;
    				}
    			}
    		}
    		return ajaxRequest;
    	}
    	
    </script>
    </head>
    
    <body>
    	<input type="button" value="send request" onclick="sendRequest()" />
    </body>
    </html>

    If i change the interval on Line # 19 from 10 to 1000, everything works fine
    Last edited by rizwan6feb; Jun 7 '08, 11:27 AM. Reason: Wanted to get the line #
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    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.

    Comment

    • rizwan6feb
      New Member
      • Jul 2007
      • 108

      #3
      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

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        please show your attempt and tell where you have problems ...

        kind regards

        Comment

        Working...