简体   繁体   English

在 ajax 成功回调上停止 ajax 加载程序

[英]Stop ajax loader on ajax success callback

I am using ajax loadmore pagination which works fine on localhost, but I am considering my method not applicable to live host.我正在使用 ajax loadmore 分页,它在本地主机上运行良好,但我认为我的方法不适用于实时主机。 This is what i am doing:这就是我正在做的事情:


     $(".button").click(function(){
       setTimout(function(){
         $('#loader').show();
         $.ajax({
           //ajax stuffs
           success:function(data){      
             $("loadmore").append(data).
           }
         });
       } ,2000);
     });

Technically speaking to me this is just taking 2 seconds to do what is inside the setTimeout function which is not right for user experience on the live server as ajax would be calling after 2 seconds.从技术上对我来说,这只需 2 秒即可完成 setTimeout function 中的操作,这不适合实时服务器上的用户体验,因为 ajax 将在 2 秒后调用。

What I want:我想要的是:

Let ajax call be made while the loader icon is loading.让 ajax 在加载程序图标加载时进行调用。 once the call is made successfully then the loader icon should stop and loadmore data should be appended.一旦调用成功,加载器图标应该停止并加载更多数据。

Something like this:像这样的东西:

$(".button").click(function(){
   $("#loader").show()
   $.ajax({
     //ajax stuffs
     success:function(data){      
     }
   });
   if(success is finished){
     $("#loader").hide()
     $("#loadmore").append(data)
   }
 });

Please how can I achieve that, thanks请问我怎样才能做到这一点,谢谢

Would this work for you?这对你有用吗?

$(".button").click(function(){
  $("#loader").show()
  $.ajax({
    //ajax stuffs
    success:function(data){      
    }
  })
  .done(function (data) {
    $("#loader").hide();
    $("#loadmore").append(data);
  }).fail(function (jqXHR, textStatus) {
      // do something
  })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM