Last active
July 10, 2024 04:30
-
-
Save shritesh/5629a28a0b9f1e8c31ea11912100fb1f to your computer and use it in GitHub Desktop.
Alpine.js Stopwatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Alpine Stopwatch</title> | |
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script> | |
</head> | |
<body> | |
<div x-data="{ | |
start: null, | |
current: null, | |
stop: null, | |
interval: null | |
}"> | |
<button x-show="!start" @click=" | |
start = Date.now() | |
current = start | |
interval = setInterval(() => {current = Date.now()}, 100) | |
">Start</button> | |
<div x-show="current"> | |
<button @click=" | |
current = null | |
stop = Date.now() | |
clearInterval(interval) | |
">Stop</button> | |
<p><span x-text="(current - start) / 1000"></span> Seconds</p> | |
</div> | |
<div x-show="stop"> | |
<button @click=" | |
start = null | |
stop = null | |
">Reset</button> | |
<p><span x-text="(stop - start) / 1000"></span> Seconds</p> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We used @shritesh's superb gist as the basis for our

Alpine.js
+Tailwind CSS
Stopwatch:https://dwyl.github.io/learn-alpine.js/stopwatch.html
Code: https://github.com/dwyl/learn-alpine.js/blob/bff0a966dda25f2d68592b30501ecdec1cc1282c/stopwatch.html
Thanks again! ❤️