Skip to content

Instantly share code, notes, and snippets.

@campino2k
Created August 9, 2012 22:00
Show Gist options
  • Save campino2k/3308416 to your computer and use it in GitHub Desktop.
Save campino2k/3308416 to your computer and use it in GitHub Desktop.
Stoppuhr-Script
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<!--
Copyright 2012 Christian Jung
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
der GNU General Public License, wie von der Free Software Foundation,
Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren
veröffentlichten Version, weiterverbreiten und/oder modifizieren.
Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
Siehe die GNU General Public License für weitere Details.
Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
-->
<title>Stoppuhr</title>
<script>
var StopWatch = function() {
var self = this;
var startTime,
showTime,
endTime,
timeDifference = 0;
var timer,
activeTimer = false;
var hours = 0;
var minutes = 0;
var seconds = 0;
this.getTime = function() {
/**
* Convert Milliseconds in different variables to set fields e.g.
* derived from http://stackoverflow.com/a/8528531
*/
t = timeDifference;
var cd = 24 * 60 * 60 * 1000,
ch = 60 * 60 * 1000,
d = Math.floor(t / cd),
h = '' + Math.floor( (t - d * cd) / ch),
m = '' + Math.round( (t - d * cd - h * ch) / 60000);
hours = h;
minutes = m;
document.getElementById( 'hours' ).value = hours;
document.getElementById( 'minutes' ).value = minutes;
return {
hours: hours,
minutes: minutes,
seconds: seconds
}
}
this.startWatch = function() {
window.clearTimeout( self.timer );
startTime = new Date();
self.activeTimer = true;
this.refreshTime();
}
this.stopWatch = function() {
window.clearTimeout( self.timer );
self.activeTimer = false;
this.getTime();
}
this.resetWatch = function() {
timeDifference = 0;
this.stopWatch();
}
this.startstop = function() {
if( !self.activeTimer ) {
this.startWatch();
} else {
this.stopWatch();
}
}
this.refreshTime = function() {
showTime = new Date();
timeDifference += ( showTime - startTime );
document.getElementById( 'showtime' ).innerHTML = timeDifference;
self.timer = window.setTimeout( self.refreshTime, 100 );
}
}
var x=new StopWatch;
</script>
</head>
<body>
<form>
<fieldset>
<legend>Zeit</legend>
<label>Stunden: <input id="hours" name="hours" /></label>
<label>Minuten: <input id="minutes" name="minutes" /></label>
<button onclick="x.startstop();return false;">Start/Stop</button>
<button onclick="x.resetWatch();return false;">reset</button>
<div id="showtime"></div>
</fieldset>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment