如何将setTimeout与嵌套递归函数一起使用?

时间:2014-04-29 15:26:37

标签: javascript recursion settimeout

前提: 创建一个伪文件上传器进度条,其中包含文件列表以及它们是否“大”。基于这些参数,它递归地循环遍历每个文件,并且在每个文件中递归循环以将文件的百分比更新为10的块作为文件数量的一部分,尽管当它添加%百分比时块依赖于它大/小。因为Javascript没有延迟/睡眠,所以我在这里查看setTimeout的实现,但是它们不处理在最里面的方法中具有setTimeout的嵌套递归方法:

setTimeout() on recursive function within a self invoking function
How to use setInterval or setTimeout with a for loop?

我实施的当前问题包括:

  • 文件编号以状态
  • 向后完成
  • 执行状态更新时缺少文件#1
  • 实际上没有发生延迟

如何更改当前实现以修复我在嵌套递归中尝试解决的这些问题? http://jsfiddle.net/BKxeF/

HTML:

<div id="example" class="k-content">
    <div class="demo-section">
        <button id="analyzeButton" class="k-button">Upload</button>
    </div>
    <div class="demo-section">
        <ul class="poll-results">
            <li>
                 <h4>File Upload Status</h4>
 <span id="progressStatus"></span>

                <div id="testProgBar"></div>
            </li>
        </ul>
    </div>
</div>



使用Javascript:

        $(document).ready(function () {
            $("#testProgBar").kendoProgressBar({
                value: 0
            });
        });

var smoothCount = 10;
        function printSmooth(percentPortion, bigType, currentFile) {

            if (smoothCount !== 0) {
                var timeDelay = (bigType[currentFile] === true) ? 3800 : 2100;

                var microPortion = (percentPortion / 10);
                var currentPortion = $("#testProgBar").data("kendoProgressBar").value();
                $("#testProgBar").data("kendoProgressBar").value(currentPortion + microPortion);

                $('#progressStatus').text("Loading file #" + (currentFile + 1));
                smoothCount = smoothCount - 1;
                setTimeout(function () {
                    printSmooth(percentPortion, bigType, currentFile);
                }, timeDelay);
            } else {
                done = true;
                return done;
            }

        }

        function printPercentAndFile(currentFile, totalFiles, bigType) {
            var percentPortion = (85 / totalFiles);
            debugger;
            //var currentPercent = ((85 / totalFiles) * currentFile);

            //$("#testProgBar").data("kendoProgressBar").value(currentPercent);
            //$('#progressStatus').text("Loading file #" + (currentFile + 1));

            if ((currentFile + 1) <= totalFiles) {
                var smoothDone = printSmooth(percentPortion, bigType, currentFile);
                if (smoothDone === true) {
                    ++currentFile;
                    smoothCount = 10;
                }
                printPercentAndFile(currentFile, totalFiles, bigType);
                //setTimeout(function () {
                //    printPercentAndFile(currentFile, totalFiles);
                //}, 3000);
            }
        }

        $("#analyzeButton").click(function () {
            var currentFile = 0,
                fileCount = 3,
                bigType = [true, false, true];

            printPercentAndFile(currentFile, fileCount, bigType);

        });

0 个答案:

没有答案