Hi Christopher.
I've get your code and found some async mistakes.
Fixed it and currently code is:
precode
var clsStopwatch = function() {
Code: Select all
// Private vars
var startAt = 0; // Time of last start / resume. (0 if not running)
var lapTime = 0; // Time on the clock when last stopped in milliseconds
var now = function() {
return (new Date()).getTime();
};
// Public methods
// Start or resume
this.start = function() {
startAt = startAt ? startAt : now();
};
// Stop or pause
this.stop = function() {
// If running, update elapsed time otherwise keep it
lapTime = startAt ? lapTime + now() - startAt : lapTime;
startAt = 0; // Paused
};
// Reset
this.reset = function() {
lapTime = startAt = 0;
};
// Duration
this.time = function() {
return lapTime + (startAt ? now() - startAt : 0);
};
};
var x = new clsStopwatch();
var $time;
var clocktimer;
var onPageShow = function() {
show();
};
jQuery(document).bind("pageshow", onPageShow);
function pad(num, size) {
Code: Select all
var s = "0000" + num;
return s.substr(s.length - size);
}
function formatTime(time) {
Code: Select all
var h = m = s = ms = 0;
var newTime = '';
h = Math.floor(time / (60 * 60 * 1000));
time = time % (60 * 60 * 1000);
m = Math.floor(time / (60 * 1000));
time = time % (60 * 1000);
s = Math.floor(time / 1000);
ms = time % 1000;
newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3);
return newTime;
}
function show() {
$time = document.getElementById('time');
}
function update() {
$time.innerHTML = formatTime(x.time());
}
var clocktimer;
function start() {
show();
Code: Select all
clocktimer = setInterval(update, 100);
x.start();
}
function stop() {
x.stop();
}
function reset() {
stop();
}
/code/pre
Please replace your with given abouve.
And try to test again.
Regards.