Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created June 10, 2020 06:12
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 ZiTAL/2b071cfa4531c61d42c8ef8f6bf243e3 to your computer and use it in GitHub Desktop.
Save ZiTAL/2b071cfa4531c61d42c8ef8f6bf243e3 to your computer and use it in GitHub Desktop.
js: count down progress bar
<!DOCTYPE html>
<html>
<style>
#myProgress {
width: 100%;
background-color: #ddd;
}
#myBar {
height: 30px;
background-color: #4CAF50;
}
</style>
<body>
<h1>JavaScript Progress Bar</h1>
<div id="myProgress">
<div id="myBar" style="width: 0%"></div>
</div>
<br>
<button onclick="move()">Click Me</button>
<script>
var elem = document.getElementById("myBar");
var seconds = 5;
function move() {
elem.style.width = '0%';
var width = (1 / seconds) * 2;
var id = window.setInterval(frame, 1000 / 60);
function frame()
{
var w = window.parseFloat(elem.style.width.replace(/%/, ''));
if (w >= 100)
{
elem.style.width = '100%';
window.clearInterval(id);
}
else
{
elem.style.width = w + width + "%";
}
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment