Сообщение от leshiple
|
Будет ли тут погрешность?
|
Суди сам:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<span id="hours">00</span>:<span id="mins">00</span>:<span id="secs">00</span>:<span id="ms">000</span><br>
<button onclick="runTimer()">start</button>
<script>
var hSpan = document.getElementById('hours'),
minSpan = document.getElementById('mins'),
secSpan = document.getElementById('secs'),
msSpan = document.getElementById('ms');
function runTimer() {
var beginTime = new Date();
setInterval(function() {
var delta = new Date() - beginTime;
var hours = Math.floor(delta/3600000);
delta = delta - hours*3600000;
var mins = Math.floor( delta/60000 );
delta = delta - mins*60000;
var secs = Math.floor( delta/1000 );
delta = delta - secs*1000;
var ms = delta;
hSpan.innerHTML = format(hours, 2);
minSpan.innerHTML = format(mins, 2);
secSpan.innerHTML = format(secs, 2);
msSpan.innerHTML = format(ms, 3);
}, 50);
}
function format(num, maxNumbers) {
var numStr = num + '';
while(numStr.length < maxNumbers) {
numStr = '0' + numStr;
}
return numStr;
}
</script>
</body>
</html>