Skip to content

Instantly share code, notes, and snippets.

@lewdev
Last active August 1, 2022 10:47
Show Gist options
  • Select an option

  • Save lewdev/ae66d1cdd9ada6425ade217ef2845595 to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/ae66d1cdd9ada6425ade217ef2845595 to your computer and use it in GitHub Desktop.
In response to Google stopping their support for the in search engine Timer, I made a stop watch app which can be a good starting point project.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="container text-center">
<h1>Stop Watch</h1>
<button id="startStopBtn" class="btn btn-primary btn-lg">Start</button>
<button id="resetBtn" class="btn btn-secondary btn-lg">Reset</button>
<p id=o class="fs-1">0</p>
</div>
<script>
(() => {
let currTime = 0;
let startTime = 0;
let accrued = 0;
let interval = null;
let go = false;
const getCurrTime = () => new Date().getTime();
const startStopBtn = document.getElementById("startStopBtn");
startStopBtn.onclick = () => {
go = !go;
startStopBtn.innerHTML = go ? "Stop" : "Start";
if (go) {
startTime = new Date().getTime();
interval = setInterval(() => {
accrued += getCurrTime() - startTime;
o.innerHTML = accrued;
}, 60);
}
else clearInterval(interval);
};
resetBtn.onclick = () => o.innerHTML = accrued = 0;
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment