完成请求后刷新页面

Page refresh after the request is Done

本文关键字:刷新 请求      更新时间:2023-11-13

我有一个页面,上面有很多复选框,我写了一个JS代码,为每个页面调用PHP页面,我想在调用完成后刷新页面。。这是我的代码

$(".matchFrnds").each(function(){  //For each CheckBox
        if($(this).is(':checked')){
            var sendData= $(this).val();
             $.post('Call to PHP Page',{sendData:sendData},function(data){

            window.location.reload();
                });
        }
        });

问题是,页面在完成几个复选框后会重新加载,所以如果有60个复选框,则页面会在调用10个复选框后重新加载。我还更改了window.location.reload();的位置,但结果是一样的,我希望一旦对所有复选框的调用完成,它就会重新加载。

您可以检查完成了多少次呼叫,然后重新加载

var boxes = $(".matchFrnds:checked").length;
var calls = 0;
$(".matchFrnds").each(function(){  //For each CheckBox
        if($(this).is(':checked')){
            var sendData= $(this).val();    
            $.post('Call to PHP Page',{sendData:sendData},function(data){
               calls++; 
               if(calls >= boxes) {    
                 window.location.reload();
               }
            });
        }
  });

这真的很容易
您只需要设置一个计数器,然后在最后一个计数器上调用reload()
这个想法是有另一个变量。。。

// Save the element to iterate in a variable
var matchFrnds = $(".matchFrnds"),
    //and save its length too
    length = matchFrnds.length;
//Iterate over it
matchFrnds.each(function() {
    // modify the counter
    --length;
    if ($(this).is(":checked")) {
        // do your things
        $.post( ... , function(data) {
            //do what you need to do with the data...
            // ....
            // ....
        });
    }
    //and, if it's the last element
    if (!length) {
        // LAST ITERATION!
        window.location.reload();
    }
});

就是这样。

您可以使用.ajaxStop事件

http://api.jquery.com/ajaxStop/

您可以在完成请求后尝试使用此功能:

location.reload();