<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#output {
width: 100px;
height: 50px;
background-color: #ccc;
border: 1px solid #999;
}
</style>
</head>
<body>
<p id="output"></p>
<div id="controls">
<button id="startPause" onclick="startPause()">Start</button>
<button onclick="reset()">Reset</button>
<button onclick="lap()">Lap</button>
</div>
<div>
<ol id="records"></ol>
</div>
<script>
var time = 0;
var running = 0;
var labs = [];
function startPause() {
if(running == 0) {
running = 1;
increment();
document.getElementById('startPause').innerHTML = "Pause";
} else {
running = 0;
document.getElementById("startPause").innerHTML = "Resume";
}
}
function reset() {
running = 0;
time = 0;
document.getElementById('startPause').innerHTML = "Start";
document.getElementById('output').innerHTML = "00:00:00";
}
function increment() {
if(running == 1) {
setTimeout(function() {
time++;
var mins = Math.floor(time/10/60);
var secs = Math.floor(time/10);
var tenths = time % 10;
if(mins < 10) {
mins = "0" + mins;
}
if(secs < 10) {
secs = "0" + secs;
}
document.getElementById('output').innerHTML = mins + ":" + secs + ":" + "0" + tenths;
increment();
}, 100);
}
}
function lap() {
document.getElementById('records').innerHTML += "<li>"+ document.getElementById('output').innerHTML +"</li>";
}
</script>
</body>
</html>
Comments