简体   繁体   English

调用函数直到jQuery .post完成加载

[英]Calling a function until jQuery .post finishes loading

I'm a little new to jQuery framework and while using AJAX with normal javascript I used readyState() function to display a loading gif image. 我是jQuery框架的新手,在将AJAX与常规javascript一起使用时,我使用readyState()函数来显示加载的gif图像。 But, I don't know how to use that in jQuery .post() method. 但是,我不知道如何在jQuery .post()方法中使用它。 Was it possible to add a class until it finishes loading? 在加载完成之前是否可以添加一个类? If so, please give a code sample. 如果是这样,请提供代码示例。 My function is similar to this: 我的功能与此类似:

$.post("verify.php",{
username: u,
password: p
},function(r) {
   if(r == 1) {
     $(".elmt").addClass("loading");
   } else if (r == 0) {
     location.href = 'http://localhost';
   }
});

Just call the addClass before the $.post() and be done with it 只需在$.post()之前调用addClass并完成它

$(".elmt").addClass("loading");
$.post("verify.php", {
    username: u,
    password: p
}, function (r) {
    location.href = 'http://localhost';
});

You could fire a custom event before starting your AJAX request. 您可以在启动AJAX请求之前触发自定义事件。 Then in your success function, fire another to stop. 然后在您的成功功能中,触发另一个以停止。

Or if you just want the loading animation: 或者,如果您只想要加载动画:

$(".elmt").addClass("loading");

$.post("verify.php",{
username: u,
password: p
},function(r) {       
     $(".elmt").removeClass("loading");
     // etc...
});

I always prefer using $.ajax for things like this as it has more options than the shortcuts : 我总是喜欢将$.ajax用于此类操作,因为它比快捷方式具有更多的选择:

$.ajax({
    type: 'POST',
    url : 'verify.php',
    data: {
           username: u,
           password: p
    },
    beforeSend: function () {
        $(".elmt").addClass("loading"); // add loader
    }
}).always(function() { // always executed

    $(".elmt").removeClass("loading"); // remove loader

}).done(function(r) { // executed only if successful
    if (r == 0) {
        location.href = '/';
    }
});

There is a global way to do this using ajaxStart() and ajaxStop(). 使用ajaxStart()和ajaxStop()有一种全局的方法来执行此操作。 See How to show loading spinner in jQuery? 请参阅如何在jQuery中显示加载微调器?

If you need to do for all your requests. 如果您需要满足所有要求。 You could try: 您可以尝试:

$(document).ajaxStart(function(){
   $(".elmt").addClass("loading");
});

$(document).ajaxStop(function(){
  $(".elmt").removeClass("loading");
});

But it's not so cool to always display the loading when the request takes little time as it will cause the screen flicking. 但是在请求花费很少的时间时始终显示负载并不是很酷,因为这会导致屏幕闪烁。 Try: 尝试:

var timer;
$(document).ajaxStart(function(){
       timer = setTimeout(function(){
          $(".elmt").addClass("loading");
       },1500);
    });

    $(document).ajaxStop(function(){
      clearTimeout(timer);
      $(".elmt").removeClass("loading");
    });

By adding a timer, only requests that take longer than 1.5 seconds should be considered long and display a loading icon. 通过添加计时器,应仅将耗时超过1.5秒的请求视为较长并显示加载图标。

As you see on code below you can do your work on different results of post method 如您在下面的代码中看到的,您可以对post方法的不同结果进行处理

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post("example.php", function() {
alert("success");
})
.done(function() { alert("second success"); })
.fail(function() { alert("error"); })
.always(function() { alert("finished"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.always(function(){ alert("second finished"); });

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

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