In this instructional exercise, I am will demonstrate to you proper methodologies to make a commencement in JavaScript with advancement bar. Here we going to make a Progress bar with a characterized max esteem and beginning quality instated with zero.
At that point we will make a commencement. While running the commencement our advancement bar will get stacked with time.
With the end goal to make this advancement bar commencement or switch stopwatch, we have to complete a few things.
Also Read:- How to Build a Chrome Extension in JavaScript
As a matter of first importance, we need to make an advancement bar. How about we do it.
<progress value="0" max="10" id="pbar" ></progress>
Here esteem is relegated with zero since we need to begin the advancement bar counter with zero.
Max esteem is here 10. The advancement bar begins with zero and can go up to 10.
Presently we require a zone to print the commencement. Here we will give a case of a 10 to 0 commencement in JavaScript. ( ten to zero commencement in JavaScript ). So simply make a "p" territory to print the commencement.
<p id="counting">10</p>
In this p label territory, we going to print 10 and afterward 9 and 8 et cetera. That implies a commencement like 10 9 8 7 6 5 4 3 2 1 0
Also Read:- How to count the occurrence / frequency of array elements in JavaScript
You may likewise get interset in,
Base Sticky Music Player Source Code in JavaScript and CSS
Alarm Before Leaving A Web Page Using JavaScript jQuery
Commencement in JavaScript with Progress Bar – Reverse stopwatch
The underneath is the JavaScript Code
<script type="text/javascript">
var reverse_counter = 10;
var downloadTimer = setInterval(function(){
document.getElementById("pbar").value = 10 - reverse_counter;
if(reverse_counter <= 0)
clearInterval(downloadTimer);
document.getElementById("counting").innerHTML= reverse_counter;
},1000);
</script>
We have utilized setInterval capacity to call a capacity at indicated interims.
1000 is our interim time in milliseconds or we can state it as a one-moment interim.
Also Read:- What is difference between Node.JS and Express.JS?
As it is a commencement of 10 seconds in this precedent so advance bar max incentive to be 10 and interim to be 1 seconds. In this manner 10/1= 10, You can either set the advancement bar max incentive to 100 and interim to be 100 milliseconds. The outcome will be the equivalent yet the thing that matters is that for this situation, the commencement will be from 100 to 0 and an opportunity to execute the commencement will be 10 seconds.
I will propose you to change the estimations of interim time and advancement bar max incentive to perceive how this is functioning. It will assist you with understanding effortlessly.
Advancement ban Countdown from 10 to 0 utilizing catch onclick in JavaScript
The underneath is the case of the basic commencement with onclick catch. Simply test it on your program and adjust according to your desire to have some good times.
Also Read:- Most important metrics you aren’t measuring: User Experience Design
Here is the full code.
<!DOCTYPE html>
<html>
<head>
<title>CountDown</title>
</head>
<body>
<progress value="0" max="10" id="pbar" ></progress><p id="counting">10</p>
<button id="start" onclick="start_countdown()">Start CountDown</button>
<script type="text/javascript">
function start_countdown(){
var reverse_counter = 10;
var downloadTimer = setInterval(function(){
document.getElementById("pbar").value = 10 - --reverse_counter;
if(reverse_counter <= 0)
clearInterval(downloadTimer);
document.getElementById("counting").innerHTML= reverse_counter;
},1000);
}
</script>
</body>
</html>