Skip to content

Commit 88884b1

Browse files
authored
Update JsClock.js
1 parent 105bb7b commit 88884b1

1 file changed

Lines changed: 34 additions & 25 deletions

File tree

JsClock.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
/*
2-
This File Was Coded By Amin Arjmand | Email: aminarj2000@gmail.com | Site: aminarjmand.com | Github Profile : https://github.com/sibche2013
3-
*/
4-
5-
//Live Clock , Update Clock Every 1 Second
6-
var clock = setInterval(function () {
7-
jsClock();
8-
}, 1000)
1+
/*
2+
Developed by Amin Arjmand
3+
Email: aminarj2000@gmail.com | Site: aminarjmand.com | GitHub: @sibche2013
4+
*/
95

10-
//Clock Function
11-
function jsClock() {
12-
var date = new Date();
13-
document.getElementById("cas").innerHTML = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
14-
}
6+
let clockInterval;
7+
const clockElement = document.getElementById("cas");
8+
9+
// Run clock immediately on load
10+
jsClock();
11+
startClock();
12+
13+
function jsClock() {
14+
const date = new Date();
15+
const hours = String(date.getHours()).padStart(2, '0');
16+
const minutes = String(date.getMinutes()).padStart(2, '0');
17+
const seconds = String(date.getSeconds()).padStart(2, '0');
18+
19+
clockElement.innerHTML = `${hours}:${minutes}:${seconds}`;
20+
}
1521

16-
//Stop & Start Clock Function
17-
function toggleClock() {
18-
if (clock > 0) {
19-
clearInterval(clock);
20-
//clearInterval Only Stop The Work, We Should 0 Clock OurSelf
21-
clock = 0;
22-
} else {
23-
setInterval(function () {
24-
jsClock();
25-
}, 1000)
26-
}
27-
}
22+
function startClock() {
23+
clockInterval = setInterval(jsClock, 1000);
24+
}
25+
26+
function toggleClock() {
27+
if (clockInterval) {
28+
clearInterval(clockInterval);
29+
clockInterval = null;
30+
clockElement.classList.add("stopped");
31+
} else {
32+
jsClock(); // Update immediately on resume
33+
startClock();
34+
clockElement.classList.remove("stopped");
35+
}
36+
}

0 commit comments

Comments
 (0)