笑故挽风 2017-10-03 08:56 采纳率: 100%
浏览 201

在For循环中延迟jQuery Ajax

I have problem to make delays between ajax requests in loop. I want script to wait 7 seconds to do next one. Requests are not identical, and I don't know how many of them can be.

$(document).ready(function () {
    var announce = $("#announce").data('id');
    var lots_cnt = parseInt($("#announce").data('lotscnt'));
    for (var i = 0; i < Math.ceil(lots_cnt/20); i++) {
        $.ajax({
                method: "GET",
                url: "/project/lots/"+announce+"/"+(i+1),
                async: false,
                beforeSend: function () {
                    $("#console").append("<strong>Parsing lots from page "+(i+1)+"...</strong><br/>");
                },
                complete:function(){

                },
                success: function (m) {
                    $("#console").append(m);
                    addprogressstep();
                    setTimeout(function() { $("#console").append("Waiting 7 sec ...<br/>"); }, 7000);
                },
                error:function(jqXHR,  textStatus,  errorThrown){

                }
            });
    };

});
  • 写回答

2条回答 默认 最新

  • weixin_33726943 2017-10-03 09:07
    关注

    This is a fantastic question!

    I see you're using jQuery .ajax. According to jQuery documentation, $.get() now returns a promise. We can use promises to acheive what you want.

    Firstly, in your for loop, for (var i = 0; i < Math.ceil(lots_cnt/20); i++) {, the first thing you do is run $.ajax. Instead of that, what we're going to do is build an array of functions, where each function returns a promise.

    var funcArray = [];
    // EDIT changed var i to let i
    for (let i = 0; i < Math.ceil(lots_cnt/20); i++) {
        var getFunction = function(){
            var getPromise = $.get(...);
            return getPromise;
        }
        funcArray.push(getFunction);
    }
    

    Then, you're going to write a recursive(ish) function to process each function, when the previous one finishes (and after a timeout if you like)

    function runNext(){
        if (funcArray.length > 0) {
            var nextFunction = funcArray.shift();
            nextFunction() // this is our $.get promise
                .then(function(resultOfGet){
                    // do something with your result
                    setTimeout(runNext,1000*7);
                })
        }
    }
    
    runNext();
    

    --- edit --- Here's how you might turn $.ajax into a promise:

    function ajx(i) {
        return new Promise(function(resolve, reject){
            $.ajax({
                method: "GET",
                url: "/project/lots/"+announce+"/"+(i+1),
                async: false,
                beforeSend: function () {
                    $("#console").append("<strong>Parsing lots from page "+(i+1)+"...</strong><br/>");
                },
                complete:function(){
    
                },
                success: function (m) {
                    $("#console").append(m);
                    addprogressstep();
                    resolve(m);
                },
                error:function(jqXHR,  textStatus,  errorThrown){
                    reject(jqXHR,  textStatus,  errorThrown);
                }
            });
        })    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器