Таймер
По учебнику
by ZondArt
HTML
<p>
<span id="time">00:00:00</span>
</p>
<input type="button" value="start" id="start">
<input type="button" value="stop" id="stop">
<input type="button" value="alert" id="alert">
JavaScript
var Clock = function(options){
var self = this;
var $elem = options.elem;
this.start = function(){
timer = setInterval(function() {
var date = new Date();
var h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var time = h + ":" + m + ":" + s;
$elem.text(time);
}, 500);
console.log(timer);
};
this.stop = function(){
clearInterval(timer);
}
this.myAlert = function(){
alert(123);
}
};
var clock = new Clock({elem: $("#time")});
$("#start").on("click", clock.start);
$("#stop").on("click", clock.stop);
$("#alert").on("click", clock.myAlert);