Stop Watch in Javascript programming language.
In this javascript tutorial, we are going to learn how to make a start-stop watch counter timer in a javascript programming language or you can say a small stopwatch.
Introduction to Javascript [counter] stopwatch program.
First of all, this is just a simple javascript program. and I dedicate this project to beginners who want to learn. and by developing this project you will learn some new terms in a javascript programming language.
My recommendation is that first of all create this project by yourself and then try to understand my code. and to fully understand this project you should have some basic knowledge of javascript programming language.
First of all, as you see in the above-given image we need to create a button and when we click on that button the timer should need to start and if again we click on the button then the timer needs to stop. and also remember that the time is increased only in seconds.
Code of program
<html>
<head>
<style type="text/css">
.first {
top: 40%;
left: 40%;
position: absolute;
}
</style>
</head>
<body class="first">
<button id="start">Start</button>
<p><span id="counter">0</span> elapsed second(s)</p>
<script type="text/javascript">
//Write-Your-Code-Here
const buttonElement = document.querySelector("button");
const counterElement = document.getElementById("counter");
// Global variable to access it from function
let intervalId = null;
// Chronometer state, initially stopped
let started = false;
buttonElement.addEventListener("click", () => {
if (!started) {
// Start the chronometer: add 1 to the counter each second
intervalId = setInterval(() => {
counterElement.textContent = String(Number(counterElement.textContent) + 1);
}, 1000);
// Update button text
buttonElement.textContent = "Stop";
} else {
// Stop the chronometer
clearInterval(intervalId);
// Update button text
buttonElement.textContent = "Start";
}
// Change chronometer state
started = !started;
});
</script>
</body>
</html>
Explanation
Here first of all we have created a button Start. and then we create a paragraph. after that using javascript, we select the button and then set an event listener. and then increasing the value of span by 1 on the time interval of 1 second. also if we click on the button then the text is changed from start to stop and again when we click it stops.
[br]
Also, read
LeCoupon: актуальные события для любителей модного шоппинга
Лента новостей, события, актуальные образы, эвенты, дропы, подиум.
https://qrmoda.ru/
Точно трендовые новинки подиума.
Абсолютно все события всемирных подуимов.
Модные дома, торговые марки, высокая мода.
Свежее место для стильныех хайпбистов.
https://fashion5.ru/
Полностью стильные новости модного мира.
Все новости самых влиятельных подуимов.
Модные дома, лейблы, гедонизм.
Самое приятное место для трендовых людей.
https://rfsneakers.ru
Абсолютно важные события моды.
Важные события всемирных подуимов.
Модные дома, лейблы, haute couture.
Новое место для трендовых людей.
https://modavmode.ru
Абсолютно трендовые события моды.
Абсолютно все события мировых подуимов.
Модные дома, лейблы, haute couture.
Свежее место для стильныех хайпбистов.
https://miramoda.ru
Абсолютно важные события моды.
Актуальные новости всемирных подуимов.
Модные дома, торговые марки, haute couture.
Самое лучшее место для стильныех людей.
https://sofiamoda.ru
Несомненно трендовые новости мира fashion.
Абсолютно все события мировых подуимов.
Модные дома, торговые марки, гедонизм.
Самое лучшее место для трендовых людей.
https://fashionsecret.ru