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>
    
    评论

报告相同问题?