Click the page to start/stop the timer. The stopwatch is accurate to the nearest millisecond. Demo here.
-
-
Save molarmanful/6cb57e19e30b529a97be to your computer and use it in GitHub Desktop.
General-purpose stopwatch - 140byt.es
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
k = onclick = function(){ //event to start timer, also declare k | |
k //check if k is truthy | |
? //if truthy | |
( | |
S=new Date, //reference point for timer accuracy | |
T = setInterval( //start timer | |
"a.innerHTML = (new Date-S)/1e3" //display time to the millisecond in element with ID 'a' | |
) //no second argument - browser will default to fastest interval anyway | |
); | |
: //else | |
clearInterval(T); //stop timer | |
k=!k //toggle k | |
} |
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
k=onclick=function(){k?(S=new Date,T=setInterval("a.innerHTML=(new Date-S)/1e3")):clearInterval(T);k=!k} |
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
DO WHAT YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2015 Benjamin Pang <molarmanful.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT YOU WANT TO. |
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
{ | |
"name": "generalPurposeStopwatch", | |
"description": "Accurate to the millisecond.", | |
"keywords": [ | |
"timer", | |
"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> | |
<title>General-Purpose Stopwatch</title> | |
<p id="a">0.000</p> | |
<script> | |
k=onclick=function(){k?(S=new Date,T=setInterval("a.innerHTML=(new Date-S)/1e3")):clearInterval(T);k=!k} | |
</script> |
save two bytes by switching the ternary and assigning k
to the truthy function:
k=onclick=function(x){k?(S=new Date,k=0,T=setInterval("a.innerHTML=(new Date-S)/1e3")):(clearInterval(T),k=1)}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one. You can probably leave out the setinterval's second argument.