How to Create a Progress Bar in JavaScript

Introduction

In this tutorial we will create a How to Create a Progress Bar in JavaScript. This tutorial purpose is to provide you simple way for creating html progress bar. This will tackle the adding of a progress bar to an HTML page. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you will do it without a problem. This program is useful if you want to apply some animation for your login screen or any event trigger. I will try my best to give you the easiest way of creating this program Progress Bar. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display progress bar interface int the page. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. #progressholder{
  7. width: 100%;
  8. background-color:#ddd;
  9. }
  10.  
  11. #progress{
  12. width: 0%;
  13. height:30px;
  14. background-color:green;
  15. }
  16.  
  17. </style>
  18.  
  19. </head>
  20. <nav class="navbar navbar-default">
  21. <div class="container-fluid">
  22. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  23. </div>
  24. </nav>
  25. <div class="col-md-3"></div>
  26. <div class="col-md-6 well">
  27. <h3 class="text-primary">How to Create a Progress Bar</h3>
  28. <hr style="border-top:1px dotted #ccc;"/>
  29. <div class="col-md-3"></div>
  30. <div class="col-md-6">
  31. <button class="btn btn-primary" onclick="startProgress(this.id);" id="btn"> Start</button>
  32. <br /><br />
  33. <div id="result">
  34. <div id="progressholder">
  35. <div id="progress"><span id="percent" style="padding:5px; font-size:20px; color:#fff;">0%</span></div>
  36. </div>
  37. <center id="stat" style="display:none;"><h4>visiting Sourcecodester</h4></center>
  38. </div>
  39. </div>
  40.  
  41.  
  42.  
  43. </div>
  44. </body>
  45. <script src="script.js"></script>
  46. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will start the progress bar animation when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function progressBar(){
  2. let progress = document.getElementById('progress');
  3. let width = 0;
  4. let interval = setInterval(progressDuration, 60);
  5.  
  6.  
  7. function progressDuration(){
  8. if(width >= 100){
  9. document.getElementById("stat").style.display="block";
  10. clearInterval(interval);
  11. window.location = 'https://sourcecodester.com';
  12. }else{
  13. width++;
  14. progress.style.width = width + '%';
  15.  
  16. document.getElementById("percent").textContent=width+"%";
  17. }
  18. }
  19.  
  20. }
  21.  
  22. function startProgress(id){
  23. document.getElementById(id).style.display="none";
  24. progressBar();
  25. }

In this code we will create method call progressBar() this will be the main function for our application. We then get the value of the targeted element using the Id, set the width to 0, and create a variable called interval using setInterval() function.

To make our progress bar animate we created method called progressDuration(). We set a condition if the width reaches 100% it will trigger a certain event, otherwise will continue to increase the progress bar until it reaches 100% by incrementing it.

Output:

The How to Create a Progress Bar in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Progress Bar in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment