1

Premise: Create a pseudo file uploader progress bar that takes in a list of file(s) and whether or not they are 'big'. Based on those parameters it recursively loops through each file, and within each file it recursively loops to update the percentage of the file by chunks of 10 as a portion of the number of files, although when it adds the % percentage chunk is dependent upon it being big/small. Because Javascript has no delay/sleep, I looked at implementations of setTimeout here, however they do not deal with nested recursive methods that have setTimeout in the innermost method:

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

The current issues that I'm having with my implementation are:

  • The file numbers are done backwards in the status
  • File #1 is missing when doing the status update
  • No delay is actually occuring

How do I change my current implementation to fix these issues that I'm trying to solve in my nested recursion? 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);

        });
Community
  • 1
  • 1
Kurt Wagner
  • 3,295
  • 13
  • 44
  • 71

0 Answers0