Привет всем!
У меня есть некий стрипт, который должен отображать количество секунд в неком блоке, основа находиться ниже. Проблема возникает при запуске таймера через setTimeout
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;" charset="utf-8"/>
<title>Таймер</title>
<script>
function timer (id) {
this.display = document.getElementById(id);
this.main;
this.base = 60;
this.time = 0;
this.init = 0;
this.data = '';
this.show = true;
this.setValue = function (data) {
if (this.display) {
display.innerHTML = data || '00:00:00.00';
}
}
*!*this.clear*/!* = function () {
clearTimeout(this.main);
this.time = 0;
this.data = '';
this.show = true;
this.init = 0;
this.setValue();
};
*!*this.fixed*/!* = function (data) {
return (data < 10)?'0'+data:data;
}
*!*this.start*/!* = function () {
this.ms = this.time % 100;
this.ms = this.fixed(this.ms);
this.sec = Math.floor(this.time/100) % this.base;
this.sec = this.fixed(this.sec);
this.min = Math.floor(this.time/(100 * this.base)) % this.base;
this.min = this.fixed(this.min);
this.h = Math.floor(this.time/(100 * Math.pow(this.base,2))) % 24;
this.h = this.fixed(this.h);
this.data = this.h + ':' + this.min + ':'+this.sec+'.'+this.ms;
if (this.show == true) {
this.setValue(this.data);
var _this = this;
this.main = setTimeout(function() {
_this.start()
}, 10);
}
if (this.ms == 99 && this.sec == 59 && this.min == 59 && this.h == 23) {this.clear();}
this.time++;
};
*!*this.toggle*/!* = function () {
if (this.init == 0) {
this.start();
this.init = 1;
} else {
if (this.show == true) {
this.show = false;
} else {
this.show = true;
this.start();
}
}
};
}
window.onload = function () {
var tm = new timer('display');
document.getElementById('toggle').onclick = function () {
tm.toggle();
};
document.getElementById('clear').onclick = function () {
tm.clear();
};
}
</script>
</head>
<body>
<div id="display"></div>
<input type="button" id="toggle" value="Start/Pause" />
<input type="button" id="clear" value="Reset" />
</body>
</html>