Javascript 等待异步响应时显示加载程序

Javascript 等待异步响应时显示加载程序,javascript,php,ajax,Javascript,Php,Ajax,我不知道在等待javascript编写的函数响应时如何显示加载程序 我有一个,其中包含响应: <input type="button" name="btn-s" value="Search"> <div align="center" id="div_feed_data"> </div> 现在我想放置一个加载gif而不是按钮,直到响应准备好,但我不知道怎么做,因为我已经找到了纯ajax代码的方法,我在js方面不如英语好,所以我需要帮助,谢谢大家 见: 见: 创

我不知道在等待javascript编写的函数响应时如何显示加载程序

我有一个
,其中包含响应:

<input type="button" name="btn-s" value="Search">
<div align="center" id="div_feed_data">
</div>
现在我想放置一个加载gif而不是按钮,直到响应准备好,但我不知道怎么做,因为我已经找到了纯ajax代码的方法,我在js方面不如英语好,所以我需要帮助,谢谢大家

见:

见:


创建Ajax请求时,它将经历以下状态

0: request not initialized 
1: server connection established
2: request received 
3: processing request 
4: request finished and response is ready
当您触发.send()方法时,将处理这些状态。因此,您尝试显示加载程序gif一旦调用Ajax调用,当就绪状态为4时,您可以隐藏加载程序。

伪代码

function yourFunction() {
   //call the show loader function here
   if(xmlReq.readyState == 4 && smlReq.status == 200){
        //call the hide loader function here
   }
}
function yourFunction() {
   if(xmlReq.readyState == 1 ){
        //call the show loader function here
        //console.log('connected');
   }

   if(xmlReq.readyState == 2 ){
        //call the show loader function here
        //console.log('Request Received');
   }

    if(xmlReq.readyState == 3 ){
        //call the show loader function here
        //console.log('Processing');
   }

   if(xmlReq.readyState == 4 && xmlReq.status == 200){
        //call the hide loader function here
   }
}
注意:您甚至可以基于readystate值显示每个状态的消息

伪代码

function yourFunction() {
   //call the show loader function here
   if(xmlReq.readyState == 4 && smlReq.status == 200){
        //call the hide loader function here
   }
}
function yourFunction() {
   if(xmlReq.readyState == 1 ){
        //call the show loader function here
        //console.log('connected');
   }

   if(xmlReq.readyState == 2 ){
        //call the show loader function here
        //console.log('Request Received');
   }

    if(xmlReq.readyState == 3 ){
        //call the show loader function here
        //console.log('Processing');
   }

   if(xmlReq.readyState == 4 && xmlReq.status == 200){
        //call the hide loader function here
   }
}

创建Ajax请求时,它将经历以下状态

0: request not initialized 
1: server connection established
2: request received 
3: processing request 
4: request finished and response is ready
当您触发.send()方法时,将处理这些状态。因此,您尝试显示加载程序gif一旦调用Ajax调用,当就绪状态为4时,您可以隐藏加载程序。

伪代码

function yourFunction() {
   //call the show loader function here
   if(xmlReq.readyState == 4 && smlReq.status == 200){
        //call the hide loader function here
   }
}
function yourFunction() {
   if(xmlReq.readyState == 1 ){
        //call the show loader function here
        //console.log('connected');
   }

   if(xmlReq.readyState == 2 ){
        //call the show loader function here
        //console.log('Request Received');
   }

    if(xmlReq.readyState == 3 ){
        //call the show loader function here
        //console.log('Processing');
   }

   if(xmlReq.readyState == 4 && xmlReq.status == 200){
        //call the hide loader function here
   }
}
注意:您甚至可以基于readystate值显示每个状态的消息

伪代码

function yourFunction() {
   //call the show loader function here
   if(xmlReq.readyState == 4 && smlReq.status == 200){
        //call the hide loader function here
   }
}
function yourFunction() {
   if(xmlReq.readyState == 1 ){
        //call the show loader function here
        //console.log('connected');
   }

   if(xmlReq.readyState == 2 ){
        //call the show loader function here
        //console.log('Request Received');
   }

    if(xmlReq.readyState == 3 ){
        //call the show loader function here
        //console.log('Processing');
   }

   if(xmlReq.readyState == 4 && xmlReq.status == 200){
        //call the hide loader function here
   }
}

这是更准确的,所以我认为它也可以帮助更好的其他人,正因为如此,我选择这个答案是正确的。但是Machalot的答案也是正确的。这更准确,所以我认为它也能帮助更好的其他人,正因为如此,我选择了这个答案作为正确答案。但马切洛特的观点也是正确的。