Skip to content

Instantly share code, notes, and snippets.

@molarmanful
Forked from 140bytes/LICENSE.txt
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save molarmanful/6cb57e19e30b529a97be to your computer and use it in GitHub Desktop.
Save molarmanful/6cb57e19e30b529a97be to your computer and use it in GitHub Desktop.
General-purpose stopwatch - 140byt.es

General-purpose stopwatch

Click the page to start/stop the timer. The stopwatch is accurate to the nearest millisecond. Demo here.

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
}
k=onclick=function(){k?(S=new Date,T=setInterval("a.innerHTML=(new Date-S)/1e3")):clearInterval(T);k=!k}
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.
{
"name": "generalPurposeStopwatch",
"description": "Accurate to the millisecond.",
"keywords": [
"timer",
"stopwatch"
]
}
<!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>
@atk
Copy link

atk commented Jul 6, 2015

Nice one. You can probably leave out the setinterval's second argument.

@aemkei
Copy link

aemkei commented Jul 19, 2015

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