停止在for循环中执行ajax


                    

我正在尝试执行以下操作。 从API获取页面数。 每个页面都有多个结果。 我会根据自己的情况检查所有结果。 如果结果符合条件,那么我需要完成检查,完成页面搜索并将结果传递给另一个函数。 我不明白如何结束ajax(在checkPages()函数中执行getData()执行)并在同一位置退出for循环。 break和return关键字无济于事。 请告诉我该怎么做。 也许我需要重构代码。 我真的不喜欢将结果从函数“扔”到函数中。 我不使用async / await,因为我需要与旧版浏览器兼容。</ p>

</ p>



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

函数getData(url){
     返回$ .get({
         网址:“要获取数据的网址”
     })
}

函数checkPages(pagesCount){
     for(var i = 2; i <= pagesCount; i ++){
         getData(“ url-to-get-data”,i).done(function(result){
             var today = checkToday(result);
             如果(今天!= null){
                 // someMethod
                 //如何结束getData函数和for循环
             }
         });
     }
}

函数checkToday(response){
     var结果= response.results;
     今天的var =新的Date();
     var day = today.getDate();
     var month = today.getMonth();
     for(var i = 0; i          var d = new Date(results [i] .release_date);
         如果(d.getDate()===天&& d.getMonth()===月){
             返回结果[i];
         }
     }
     返回null;
} code> </ pre>
</ div>
</ div>

     </ div>

展开原文

原文

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个回答


对checkPages函数的最简单更改</ p>

根据需要自行调用的内部函数</ p>

  function checkPages(pagesCount){
     函数checkPage(i){
         如果(i <= pagesCount){
             getData(“ url-to-get-data”,i).done(function(result){
                 var today = checkToday(result);
                 if(today == null){//仅当今天为null时才获取下一页
                     checkPage(i + 1);
                 }
             });
         }
     }
     checkPage(2);
}
</ code> </ pre>
     </ div>

展开原文

原文

simplest change to your checkPages function

inner function that calls itself as required

function checkPages(pagesCount) {
    function checkPage(i) {
        if (i <= pagesCount) {
            getData("url-to-get-data", i).done(function(result) {
                var today = checkToday(result);
                if (today == null) { // only get next page if today is null
                    checkPage(i+1);
                }
            });
        }
    }
    checkPage(2);
}


如果我理解正确,您正在尝试执行类似的操作?</ p>

更新:已实施que以检查请求是否已完成</ p>

</ p>



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

函数getData(url){
     返回$ .get({
         网址:“要获取数据的网址”
     })
}

函数checkPages(pagesCount){
     让doContinue = true;
     让loading = false;
     让我= 2;
     var checker = setTimeout(()=> {
       if(i> pagesCount)clearTimeout(checker);
       if(!loading){
           loading =真;
           getData(“ url-to-get-data”,i).done(function(result){
             var today = checkToday(result);
             如果(今天!= null){
                 clearTimeout(checker);
             }
             i ++;
             加载=假;
         });
       }
     } 100);
}

函数checkToday(response){
     var结果= response.results;
     今天的var =新的Date();
     var day = today.getDate();
     var month = today.getMonth();
     for(var i = 0; i          var d = new Date(results [i] .release_date);
         如果(d.getDate()===天&& d.getMonth()===月){
             返回结果[i];
         }
     }
     返回null;
} code> </ pre>
</ div>
</ div>

     </ div>

展开原文

原文

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>


使ajax调用同步或使用回调函数来不断获取更多数据,直到满足条件为止。</ p>
     </ div>

展开原文

原文

Make your ajax call synchronous or use callback functions to keep getting more data until conditions are met.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐