其他功能完成后如何使功能运行

时间:2013-10-12 09:48:00

标签: javascript

var showBoth = function(str, x) {
       showgrade(str, x);
       showsize(str, x);
     }

我试过这个:

var showBoth = function(str, x) {`enter code here`
        showgrade(str, x);
        setTimeout(function(){showsize(str, x)},500);
     }

settime如果showgrade占用太多时间会停止并运行showsize。{/ p>

showsize完成运行后如何运行showgrade

这是我的全部功能

 function showsize(str,x)
    {
        if (str=="")
          {
          document.getElementById("txtHintsize"+x+"").innerHTML="";
          return;
          } 
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("txtHintsize"+x+"").innerHTML=xmlhttp.responseText;
            }
          }

        xmlhttp.open("GET","Function/ShowSize.php?q="+str,true);
        xmlhttp.send();
    }
    function showgrade(str,x)
    {
        if (str=="")
          {
          document.getElementById("txtHintgrade"+x+"").innerHTML="";
          return;
          } 
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("txtHintgrade"+x+"").innerHTML=xmlhttp.responseText;
            }
          }

        xmlhttp.open("GET","Function/ShowGrade.php?q="+str,true);
        xmlhttp.send();
    }
     var showBoth = function(str, x) {
        showgrade(str, x);
        setTimeout(function(){showsize(str, x)},500);
     }

2 个答案:

答案 0 :(得分:1)

像这样更改showgrade功能的xmlhttp.onreadystatechange

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("txtHintgrade" + x + "").innerHTML = xmlhttp.responseText;
    }
    showsize(str, x);
}

当AJAX请求返回响应时,它将调用onreadystatechange函数,这将标记函数的结尾。因此,我们只需从该函数调用showsize

答案 1 :(得分:0)

试试这个:

var showBoth = function(str, x) {`enter code here`
    showgrade(str, x, function(){ showsize(str, x); });
 }

然后将您的showgrade更改为:

function showgrade(str, x, callback){
   //DO WHAT YOU WANT HERE
   callback.call();
}