Skip to content

Instantly share code, notes, and snippets.

@mostaphaRoudsari
Created March 30, 2015 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mostaphaRoudsari/ee970a02cfd560804876 to your computer and use it in GitHub Desktop.
Save mostaphaRoudsari/ee970a02cfd560804876 to your computer and use it in GitHub Desktop.
An example of updating progress bar inside multiple loops. I assume there should be a smarter way to do it but for now this works for me
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="col-xs-12">
<progress style="width:50%" value="0" max="100" id="progBar">
<span id="calcProgress">
<!-- Fallback goes here -->
</span>
</progress>
</div>
<div id="progUpdate" class="col-xs-12">
<!-- Success Message -->
</div>
<script type="text/javascript">
var counter = 0;
var rounds = [0, 1];
var cityNames = ["New York", "Chicago", "Dalas"];
var poi = ["Musuem", "Library", "Park", "Office"];
totalCases = rounds.length * cityNames.length * poi.length;
function myFunction() {
var roundIndex = parseInt(counter/(cityNames.length * poi.length))
var cityIndex = parseInt((counter/poi.length)%cityNames.length);
var poiIndex = parseInt(counter%poi.length);
// do stuff
counter++;
var progress = ((counter/totalCases)* 100).toFixed(1);
console.log("Round " + rounds[roundIndex] + "> " + cityNames[cityIndex] + " " + poi[poiIndex]);
$('#progUpdate').empty().append("(" + progress + "%) Round " + rounds[roundIndex] + "> " + cityNames[cityIndex] + " " + poi[poiIndex]);
$('#progBar').val((counter/totalCases) * 100);
if (counter < totalCases) {
setTimeout(myFunction, 100);
}
};
myFunction();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment