BillGilbert

progress bar

Jun 13th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.59 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4.     #myProgress {
  5.         width: 100%;
  6.         background-color: #ddd;
  7.     }
  8.  
  9.     #myBar {
  10.         width: 1%;
  11.         height: 30px;
  12.         background-color: #4CAF50;
  13.     }
  14. </style>
  15. <body>
  16.  
  17. <h1>JavaScript Progress Bar</h1>
  18.  
  19. <div id="myProgress">
  20.     <div id="myBar"></div>
  21. </div>
  22.  
  23. <br>
  24. <button onclick="move()">Click Me</button>
  25.  
  26. <script>
  27.     window.onload = function() {
  28.         checkStatus();
  29.     };
  30.  
  31.     var id;
  32.     var width;
  33.     function move() {
  34.         var elem = document.getElementById("myBar");
  35.  
  36.         id = setInterval(frame, 40);
  37.  
  38.         var currentValue = localStorage.getItem('progressValue');
  39.         var currentStatus = localStorage.getItem('buttonState');
  40.  
  41.         width = 1;
  42.  
  43.         if (currentValue) {
  44.             if (currentStatus==='running') {
  45.                 width = currentValue;
  46.             }
  47.         }
  48.  
  49.         function frame() {
  50.             if (width >= 100) {
  51.                 clearInterval(id);
  52.                 localStorage.setItem('buttonState', 'stop');
  53.             } else {
  54.                 localStorage.setItem('progressValue', width);
  55.                 localStorage.setItem('buttonState', 'running');
  56.  
  57.                 width++;
  58.                 elem.style.width = width + '%';
  59.              }
  60.         }
  61.     }
  62.  
  63.     function checkStatus(){
  64.         var progressStatus = localStorage.getItem('buttonState');
  65.        
  66.         if (progressStatus==='running') {
  67.             move();
  68.         } else {
  69.             localStorage.setItem('buttonState', 'stop');
  70.         }
  71.     }
  72.  
  73. </script>
  74.  
  75. </body>
  76. </html>
Advertisement
Add Comment
Please, Sign In to add comment