-1

I have a code for the progress bar and I want the progress bar repeat itself 10 times,in other words going from 0% to 100%, 10 times. I have created a for loop but the code only runs the ProgressBarSim function once.

Here is script:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
</head>
<body>
  <script>
     var sim;
     var mm=0;
     for (i = 0; i < 10; i++) {
         var al=0;
         var t=0;
         function progressBarSim(al, t) {
            var bar = document.getElementById('progressBar');
            var status = document.getElementById('status');
            bar.value = al;
            status.innerHTML = al + "%";
            al++;
            sim = setTimeout(function(){ progressBarSim(al, t); }, 50);
            if (al == 101) {
               clearTimeout(sim);
               var al=0;
               var t=0;
            }//end if 
         }//end function
      }//end for

  </script>

  <form  name="Calculation" method="post">

  <progress id="progressBar" value="0" max="100" style="width:300px;">  </progress>
   <span id="status"></span> 
  <input type="button" id="btn2" value="Start Acquisition" onclick="progressBarSim(0, 0)">

  </form>
</body>
</html>

How can I make it to repeat itself?

5
  • Why is progressBarSim declared in the for loop? Commented Aug 12, 2015 at 13:31
  • don't you just put the entire for loop inside another for loop? Commented Aug 12, 2015 at 13:32
  • From what I read the function has to be inside the for loop. Commented Aug 12, 2015 at 13:33
  • You want to follow Pete's advice down below because you are declaring a function named progressBarSim during every iteration of the for loop. But to answer why this isn't working it is because you never actually call progressBarSim you just declare it 10 times. Commented Aug 12, 2015 at 13:40
  • ok Thanks, so what should I change? Commented Aug 12, 2015 at 13:43

2 Answers 2

4

I don't see the need for your for loop (as this is what the setTimeout does) and I'm not sure what your t variable does as you don't seem to use it anywhere (may want to get rid of it) but you can change your js to the following and it should work:

function progressBarSim(al, t) {
    var bar = document.getElementById('progressBar');
    var status = document.getElementById('status');

    bar.value = al;
    status.innerHTML = al + "%";

    al++;

    // this will call iteself up until 100 as you are passing the al var back to the same function and iterating it
    if (al < 101) {
        setTimeout(function () {
            progressBarSim(al, t); // as you are calling the function within itself, it is creating it's own loop
        }, 50);
    }
}

Example

I think you are confusing setTimeout and setInterval.

setTimeout will call the function once after the specified amount of time has passed, you wouldn't need to do a clearTimeout unless you wanted to cancel that event happening after it is called but before the elapsed time has passed.

setInterval will call the function continuously, pausing the for the amount of time specified, between each function call. Here clearInterval will stop the looping

Update as per comments and figuring out what t is for

var timesToRepeat = 10,
    timer;

function progressBarSim(al, t) {
    clearTimeout(timer); // added incase the button is clicked twice
    var bar = document.getElementById('progressBar');
    var status = document.getElementById('status');

    bar.value = al;
    status.innerHTML = al + "%";

    al++;

    // this will call iteself up until 100 as you are passing the al var in and iterating it
    if (al <= 101 && t < timesToRepeat) {
        if (al == 101) {
            al = 0;
            t++;
        }

        if (t < timesToRepeat) {
            timer = setTimeout(function () {
                progressBarSim(al, t);
            }, 50);
        }
    }
}

Updated example

Sign up to request clarification or add additional context in comments.

3 Comments

I want the progress bar to start from zero when it reaches 100% and repeat it self.
I have changed var al=0; var t=0; to progressBarSim(0, 0); and it is working now.
okay, I see what t is for now, so if you check this, you can change what timesToRepeat to the number of times you want it to repeat: jsfiddle.net/9rdv9jdj/5
1

I'm assuming mm variable is the counter to how many times you want to run it. if (mm < 10) checks if it is already run 10 times.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
</head>
<body>
    <script>
    var sim;
    var mm=1;
    function progressBarSim(al, t) {
            var bar = document.getElementById('progressBar');

            var status = document.getElementById('status');

            bar.value = al;

            status.innerHTML = al + "%";

            al++;
            sim = setTimeout(function(){ progressBarSim(al, t); }, 50);

            if (al == 101) {

                clearTimeout(sim);

                var al=0;
                var t=0;
                if(mm < 10)
                    progressBarSim(0, 0);
                mm++;
            }
        }


    </script>

    <form  name="Calculation" method="post">

    <progress id="progressBar" value="0" max="100" style="width:300px;">  </progress>
    <span id="status"></span>
    <input type="button" id="btn2" value="Start Acquisition" onclick="progressBarSim(0, 0)">

    </form>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.