$(
function
() {
var
hours = minutes = seconds = milliseconds = 0;
var
prev_hours = prev_minutes = prev_seconds = prev_milliseconds = undefined;
var
timeUpdate;
$(
"#start_pause_resume"
).click(
function
(){
if
($(
this
).hasClass(
"start"
)){
$(
this
).attr(
"class"
,
"pause glyphicon glyphicon-pause h100"
);
updateTime(0,0,0,0);
}
else
if
($(
this
).hasClass(
"pause"
)){
$(
"#record"
).prepend(
"<li id='recordNo"
+timeUpdate+
"' value='"
+minutes+
":"
+seconds+
":"
+milliseconds+
" '"
+
"class='list-group-item'> #"
+ timeUpdate +
" <span class='glyphicon glyphicon-time'></span> "
+ minutes+
" : "
+ seconds+
" : "
+milliseconds+
""
+
"<span class='glyphicon glyphicon-remove pull-right ' onclick='removeSelf("
+timeUpdate+
")'></span></li>"
);
$(
this
).attr(
"class"
,
"start glyphicon glyphicon-play h100"
);
clearInterval(timeUpdate);
}
});
$(
"#reset"
).click(
function
(){
if
(timeUpdate) clearInterval(timeUpdate);
setStopwatch(0,0,0,0);
});
function
updateTime(prev_hours, prev_minutes, prev_seconds, prev_milliseconds){
var
startTime =
new
Date();
timeUpdate = setInterval(
function
() {
var
timeElapsed =
new
Date().getTime() - startTime.getTime();
hours = parseInt(timeElapsed / 1000 / 60 / 60) + prev_hours;
minutes = parseInt(timeElapsed / 1000 / 60) + prev_minutes;
if
(minutes > 60) minutes %= 60;
seconds = parseInt(timeElapsed / 1000) + prev_seconds;
if
(seconds > 60) seconds %= 60;
milliseconds = timeElapsed + prev_milliseconds;
if
(milliseconds > 1000) milliseconds %= 1000;
setStopwatch(hours, minutes, seconds, milliseconds);
}, 25);
}
function
setStopwatch(hours, minutes, seconds, milliseconds){
$(
"#minutes"
).html(prependZero(minutes, 2));
$(
"#seconds"
).html(prependZero(seconds, 2));
$(
"#milliseconds"
).html(prependZero(milliseconds, 2));
}
function
prependZero(time, length) {
time =
new
String(time);
return
new
Array(Math.max(length - time.length + 1, 0)).join(
"0"
) + time;
}
});