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