Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "Stop Watch" project using HTML, CSS & JavaScript.
Preview
In the above video, you’ve seen the preview of the "Stop Watch" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS and JavaScript code below.
Stop Watch [Source Code]
To get the following HTML, CSS & JS code for the Stop Watch project. You need to create three files one is a HTML file, second one is a CSS file and the another one is JS file. After creating these three files then you can copy-paste the given codes on your document.
Remember, you’ve to create a file with .html extension for HTML code, .css extension for CSS code and .js for JavaScript code.
You can also download all source code files from the given download button.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Stop Watch</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="stopWatch"> <h1 class="time">00:00:00</h1> <div class="controls"> <img src="images/stop.png" onclick="stopTimer()" alt="stop"> <img src="images/start.png" onclick="startTimer()" alt="start"> <img src="images/reset.png" onclick="resetTimer()" alt="reset"> </div> </div> <script src="index.js"></script> </body> </html>
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { background-color: white; } .stopWatch { background-image: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(images/background.png); max-width: 600px; background-size: cover; background-position: center; padding: 40px 0; margin: 200px auto 0; text-align: center; color: white; } .stopWatch h1 { font-size: 80px; margin: 50px 0; } .controls { display: flex; align-items: center; justify-content: center; } .stopWatch img { width: 60px; margin: 0 30px; cursor: pointer; } .stopWatch img:nth-child(2) { width: 90px; }
let timeEl = document.querySelector(".time"); let timer = null; let [hours,minutes,seconds] = [0,0,0]; function stopWatch(){ seconds++; if(seconds==60){ seconds=0; minutes++; if(minutes==60){ minutes=0; hours++; } } let h = hours<10 ? ("0"+hours) : hours; let m = minutes<10 ? ("0"+minutes) : minutes; let s = seconds<10 ? ("0"+seconds) : seconds; timeEl.innerHTML = h+":"+m+":"+s; } function startTimer(){ if(timer!=null){ clearInterval(timer); } timer = setInterval(stopWatch,1000); console.log(timer) } function stopTimer(){ clearInterval(timer); console.log(timer) } function resetTimer(){ clearInterval(timer); [hours,minutes,seconds] = [0,0,0]; timeEl.innerHTML = "00:00:00"; console.log(timer) }