我根据Jory Hogeven的评论修改了代码,解决了我的问题。我要做的是,不使用常规循环,而是使用一个递归函数,该函数在完成请求调用后调用自己。对于那些正在寻找相同答案的人,请参见下文。
var users_info = <?php echo json_encode($usersarray); ?>;
console.log(users_info);
$=jQuery;
var each = \'\';
j = 0;
function nextAjax(i) {
var data = {
\'action\': \'delete_orphan_users\',
\'user_id\': users_info[i]
};
$.post(ajaxurl, data, function(response) {
n = new Date($.now());
m = n.getHours()+\':\'+n.getMinutes();
$("#status").prepend(m+\' \'+response);
j++;
$("#deletedusers").html(j);
if( j==users_info.lenght ){
location.reload(); //current batch finished, reload screen to get another batch of users
} else {
nextAjax(j);
}
});
}
console.log(\'Start\')
nextAjax(j);