首页 > 解决方案 > 如何根据重试次数重复迭代

问题描述

我正在将文件块上传到 Dropbox,我需要在循环中添加一个简单的重试。因此,如果第一次尝试失败,请在放弃之前再重试 2 次。

给出一些上下文。我将文件分块上传到 Dropbox。但是,我需要让脚本优雅地失败。在我终止上传并给用户一个错误之前,要求脚本重复上传 3 次。

例如(不是实际尝试只是一个概念):

    var retries = 3;
    jQuery(dropbox.chunks).each(function(index, chunk){

      var result = anothingFunction();

      if (result == true) {

        //continue the loop

      }

      if (result == false) {

        retries--;

        if (retries > 0) {
            //Retry this iteration
        }

        if (retries = 0) {
            //Kill the entire loop as this upload clearly is not going to happen. 
        }

      }

    }

标签: javascriptjquery

解决方案


推荐阅读