weixin_33696822 2018-08-21 11:24 采纳率: 0%
浏览 196

停止在for循环中执行ajax

I'm trying to do the following. Get number of pages from the API. Each page has multiple results. I check all the results with my condition. If the result fits the condition, then I need to finish the check, finish the page search and pass the result to another function. I don't understand how to end ajax (getData() execution in the checkPages() function) and exit the for loop in the same place. The break and return keywords do not help. Please tell me how to do it. Maybe I need to do to refactor my code. I don't really like to "throw" results from a function into a function. I do not use async/await because I need compatibility with old browsers.

getData("url-to-get-data").done(function (result) {
    checkPages(result.total_pages);
});

function getData(url) {
    return $.get({
        url: "url-to-get-data"
    })
}

function checkPages(pagesCount) {
    for (var i = 2; i <= pagesCount; i++) {
        getData("url-to-get-data", i).done(function(result) {
            var today = checkToday(result);
            if (today != null) {
                //someMethod
                //how to end the getData function and the for loop
            }
        });
    }
}

function checkToday(response) {
    var results = response.results;
    var today = new Date();
    var day = today.getDate();
    var month = today.getMonth();
    for (var i = 0; i < results.length; i++) {
        var d = new Date(results[i].release_date);
        if (d.getDate() === day && d.getMonth() === month) {
            return results[i];
        }
    }
    return null;
}

</div>
  • 写回答

3条回答 默认 最新

  • weixin_33720186 2018-08-21 11:29
    关注

    If I understand correctly you are trying to do something like this?

    UPDATE: implemented que to check if request is finsihed

    getData("url-to-get-data").done(function (result) {
        checkPages(result.total_pages);
    });
    
    function getData(url) {
        return $.get({
            url: "url-to-get-data"
        })
    }
    
    function checkPages(pagesCount) {
        let doContinue = true;
        let loading = false;
        let i = 2;
        var checker = setTimeout(()=>{
          if(i > pagesCount) clearTimeout(checker);
          if(!loading){
              loading = true;
              getData("url-to-get-data", i).done(function(result) {
                var today = checkToday(result);
                if (today != null) {
                    clearTimeout(checker);
                }
                i++;
                loading = false;
            });
          }
        },100);
    }
    
    function checkToday(response) {
        var results = response.results;
        var today = new Date();
        var day = today.getDate();
        var month = today.getMonth();
        for (var i = 0; i < results.length; i++) {
            var d = new Date(results[i].release_date);
            if (d.getDate() === day && d.getMonth() === month) {
                return results[i];
            }
        }
        return null;
    }

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题