Skip to content

Instantly share code, notes, and snippets.

@mailtodanish
Created December 27, 2021 11:11
Show Gist options
  • Save mailtodanish/bc3d44e5b96fb649c0c421fa3c899aee to your computer and use it in GitHub Desktop.
Save mailtodanish/bc3d44e5b96fb649c0c421fa3c899aee to your computer and use it in GitHub Desktop.
stop watch in js
var timer = document.getElementById("timer");
var idealTimer = document.getElementById("ideal");
var runningId;
var idealId;
var value = "0:0";
var Idealvalue = "00:00";
function startTimer(m, s) {
value = m + ":" + s;
if (s == 60) {
m = m + 1;
s = 0;
}
s = s + 1;
timer.textContent = value;
runningId = setTimeout(function () {
startTimer(m, s)
}, 1000);
}
function idealTimer1(m, s) {
console.log(m,s);
Idealvalue = m + ":" + s;
if (s == 60) {
m = m + 1;
s = 0;
}
s = s + 1;
idealTimer.textContent = Idealvalue;
idealId = setTimeout(function () {
idealTimer1(m, s)
}, 1000);
}
function pauseTimer() {
clearTimeout(runningId);
var k = Idealvalue.split(":");
idealTimer1(parseInt(k[0], 10), parseInt(k[1], 10));
}
function resumeTimer() {
clearTimeout(idealId);
var t = value.split(":");
startTimer(parseInt(t[0], 10), parseInt(t[1], 10));
}
@mailtodanish
Copy link
Author

mailtodanish commented Dec 27, 2021

<button onclick="startTimer(0,0)">Start</button>
<button onclick="pauseTimer()">Pause</button>
<button onclick="resumeTimer()">Resume</button>
<br>
Running Time:<div id="timer">0:0</div>
Ideal Time:<div id="ideal">0:0</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment