简体   繁体   English

请求完成后页面刷新

[英]Page refresh after the request is Done

I have a page with many checkboxes on it, I wrote a JS code that makes call to PHP page for each page, I want to refresh the page after the call has completed.. Here is my code 我有一个页面上有很多复选框,我写了一个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();
                });
        }
        });

The problem is that the page reloads after completing few checboxes, so if there are 60 checkboxes, the page reloads after making call for 10 checkboxes. 问题是页面在完成几个checbox后重新加载,所以如果有60个复选框,则在调用10个复选框后页面会重新加载。 I also changed the place for window.location.reload(); 我也改变了window.location.reload(); but the results are same, I want that once the call for all the checkboxes is completed then it reloads. 但结果是一样的,我希望一旦完成所有复选框的调用,它就会重新加载。

You can check how many calls you have finished then 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();
               }

            });
        }
  });

It is really easy. 这真的很容易。
You just need to set a counter, and call reload() on the last one. 你只需要设置一个计数器,并在最后一个上调用reload()
The idea would be to have another variable... 想法是有另一个变量......

// 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();
    }
});

And that's it. 就是这样。

You could use the .ajaxStop-Event 您可以使用.ajaxStop-Event

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

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

location.reload();

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

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