-
-
Save andytwoods/46eba3ddb06f78fb20bbf648def0e7d8 to your computer and use it in GitHub Desktop.
Stopwatch in javascript
This file contains 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
Copyright (c) 2010-2015 Giulia Alfonsi <[email protected]> | |
Modified work Copyright 2017 Andy Woods | |
Permission is hereby granted, free of charge, to any person | |
obtaining a copy of this software and associated documentation | |
files (the "Software"), to deal in the Software without | |
restriction, including without limitation the rights to use, | |
copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the | |
Software is furnished to do so, subject to the following | |
conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. |
This file contains 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>Stopwatch</title> | |
</head> | |
<body onload="show();"> | |
<div>Time: <span id="time"></span></div> | |
<script> | |
var my_stopwatch = stopwatch('stopwatch') | |
my_stopwatch.start() | |
</script> | |
</body> | |
</html> |
This file contains 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
var stopwatch = function(my_element_id) { | |
var $time = document.getElementById(my_element_id) | |
if(!$time) return | |
var api = {} | |
var duration = 50 | |
var time = 0 | |
var clocktimer | |
var h, m, s, ms | |
function pad(num, size) { | |
var s = "0000" + String(num) | |
return s.substr(s.length - size) | |
} | |
function formatTime() { | |
time += duration | |
h = Math.floor( time / (60 * 60 * 1000) ) | |
m = Math.floor( time / (60 * 1000) % 60) | |
s = Math.floor( time / 1000 % 60 ) | |
ms = time % 1000 / 10 | |
return pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 2) | |
} | |
function update() { | |
$time.innerHTML = formatTime() | |
} | |
api.start = function() { | |
clocktimer = setInterval(update, duration) | |
} | |
api.stop = function() { | |
clearInterval(clocktimer) | |
} | |
api.formatTime = formatTime | |
return api | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In stopwatch.html line 10,
"var my_stopwatch = stopwatch('time')"