many Ajax request question

2011-05-05
2013-01-31
  • Edward King
    Edward King
    2011-05-05

    I want to send two ajax request about every two seconds,it's purpose is to refresh two graphics,the code is follows:

    function getJFreeCharName(){
       if(xmlhttp){
          var url="getCharName";
          var param="tag=1";
          xmlhttp.open("POST",url,true);
          xmlhttp.setRequestHeader("Content-Length",param.length);
          xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          xmlhttp.onreadystatechange=callBackFun;
          xmlhttp.send(param);
        }
    }
    //deal getJFreeCharName
    function callBackFun(){
       if(xmlhttp.readyState==4){
         if(xmlhttp.status==200){
           setTimeout("getJFreeCharName()",2000);
           document.getElementById("imgs").style.display="";
           document.getElementById("imgs").src=document.getElementById("hidText").value + xmlhttp.responseText;
          }
       }
    }
    function getJFreeCharName2(){
       if(xmlhttp){
          var url="getCharName";
          var param="tag=2";
          xmlhttp.open("POST",url,true);
          xmlhttp.setRequestHeader("Content-Length",param.length);
          xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          xmlhttp.onreadystatechange=callBackFun2;
          xmlhttp.send(param);
        }
    }
    //deal getJFreeCharName2
    function callBackFun2(){
       if(xmlhttp.readyState==4){
         if(xmlhttp.status==200){
           setTimeout("getJFreeCharName2()",2000);
           document.getElementById("imgs2").style.display="";
           document.getElementById("imgs2").src=document.getElementById("hidText2").value + xmlhttp.responseText;
          }
       }
    }
    getJFreeCharName();
    getJFreeCharName2();

    When I run above code,I find only getJFreeCharName2 run and getJFreeCharName don't run! Why? How to correct above code?
    Thanks

     
  • This topic isn't related to our project. But you can use some high-level JavaScript library (Prototype, jQuery, YUI, ExtJS, Mootools, Dojo etc.)