简体   繁体   English

如何等待for循环直到ajax调用结束

[英]How to wait a in a for loop until ajax call finishes

I'm new to developing web sites and I'm developing a small web site. 我是开发网站的新手,我正在开发一个小型网站。 Here I'm using ajax to show new comments. 在这里,我使用ajax来显示新评论。 this is the function I wrote 这是我写的功能

function show_comments() {
$('div#P_all_posts>div').each(function () {
    id = this.id.replace("post", "");
    $.ajax({

        url: 'http://localhost/seppro/index.php/group/show_comments',
        type: 'post',
        data: {
            id: id
        },
        success: function (data) {

           document.getElementById("cmnt"+id).innerHTML=data;

        },
        error: function (err, req) {
            alert(err)
        },
        async: false
    });

});
setTimeout("show_comments()", 5000);
}

The PHP PHP

model 模型

public function showcmnts(){ 
    $sql="select * from tbl_cmnt 
          where postid='".$this->input->post("id")."'"; 
    $query = $this->db->query($sql); return $query->result(); 
}

controller 调节器

 public function show_comments(){
    $data['cmntlist'] = $this->Group_model->showcmnts();
    $this->load->view('group/grp_cmnts', $data);
}

view 视图

foreach ($cmntlist as $cmnt):
echo $cmnt->comment . "<br>";
endforeach;

even though I set async: false in the ajax success function I can only get the last id(id of the last div) inside the ajax success function.but when i alert the id(id of the div) above the ajax part i am getting the correct id . 即使我在ajax成功函数中设置了async: false ,我只能在ajax成功函数中获取最后一个id(最后一个div的id)。但是当我提醒ajax部分上方的id(id的id)时,我得到正确的身份证。 so how can I pause the loop until ajax function finishes.sorry for my bad English please help me 那么我怎么能暂停循环直到ajax功能完成。我的英语不好请帮助我

The error seems to be in the fact that you never call the function show_comments() for first time in the the script 错误似乎是因为您从未在脚本中第一次调用函数show_comments()

  1. I recommend you to use this when you call it 我建议你在打电话时使用它

    setInterval(show_comments(), 5000) setInterval(show_comments(),5000)

And remove the setTimeOut() from show_comments() 并取出setTimeOut()show_comments()

  1. Or you can just keep setTimeOut() and call show_comments() inside the script path or in the function you are going to call it 或者,你可以保持setTimeOut()和呼叫show_comments()脚本路径内或在功能你要叫它

Try to wrap your ajax call to immediately invoked function and pass id to it: 尝试将ajax调用包装到立即调用的函数并将id传递给它:

    $('div#P_all_posts>div').each(function () {
        id = this.id.replace("post", "");
        (function(id) {
            $.ajax({

                url: 'http://localhost/seppro/index.php/group/show_comments',
                type: 'post',
                data: {
                    id: id
                },
                success: function (data) {

                    document.getElementById("cmnt" + id).innerHTML = data;

                },
                error: function (err, req) {
                    alert(err)
                },
                async: false
            });
        })(id)
    });

It seems like your id var is global, and on each loop it rewrites its value, and by the time ajax is completed, id is equal to last value in loop. 看起来你的id var是全局的,并且在每个循环上它重写它的值,并且在ajax完成时, id等于循环中的最后一个值。

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

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