-
Notifications
You must be signed in to change notification settings - Fork 0
/
numSysStopWatch.html
110 lines (93 loc) · 3.95 KB
/
numSysStopWatch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>قدم شمار / گام شمار</title>
<style>
p {
font-family: 'Courier New', Courier, monospace;
font-weight: 600;
font-size: large;
}
div {
position: relative;
top: 2em;
text-align: center;
}
</style>
</head>
<body>
<h1>قدم شمار فارسی</h1>
<div>
<h2>ساعت الان به وقت محلی</h2>
<p class="clock"></p>
</div>
<div>
<h2>قدم شمار</h2>
<p class="StopWatchClass"></p>
<button class="start">شروع</button>
<button class="stop">توقف</button>
<button class="reset">بازنشانی</button>
<p>
شما میتوانید با تغییر زیر از شمارش ثانیهای به هزارم ثانیه این قدم شمار را تغییر دهید:<br>
<code><bdi>setInterval(displayCount, 1000); ==> setInterval(displayCount, 1);</bdi></code>
</p>
</div>
<script>
function displayCurrentTime() {
let date = new Date();
let time = date.toLocaleTimeString("fa-IR");
document.querySelector('.clock').textContent = time;
}
displayCurrentTime();
const createClock = setInterval(displayCurrentTime, 1);
//--StopWatch Section-- //
// Define a counter variable for the number of seconds and set it to zero.
let secondCount = 0;
// Define a global to store the interval when it is active.
let stopWatch;
// Store a reference to the display paragraph in a variable
const displayPara = document.querySelector('.StopWatchClass');
// Function to calculate the current hours, minutes, and seconds, and display the count
function displayCount() {
// Calculate current hours, minutes, and seconds
let hours = Math.floor(secondCount / 3600);
let minutes = Math.floor((secondCount % 3600) / 60);
let seconds = Math.floor(secondCount % 60)
// Display a leading zero if the values are less than ten
let displayHours = (hours < 10) ? '0' + hours : hours;
let displayMinutes = (minutes < 10) ? '0' + minutes : minutes;
let displaySeconds = (seconds < 10) ? '0' + seconds : seconds;
// Write the current stopwatch display time into the display paragraph
displayPara.textContent = displayHours + ':' + displayMinutes + ':' + displaySeconds;
// Increment the second counter by one
secondCount++;
}
// Store references to the buttons in constants
const startBtn = document.querySelector('.start');
const stopBtn = document.querySelector('.stop');
const resetBtn = document.querySelector('.reset');
// When the start button is pressed, start running displayCount() once per second using setInterval()
startBtn.addEventListener('click', () => {
stopWatch = setInterval(displayCount, 1000);
startBtn.disabled = true;
});
// When the stop button is pressed, clear the interval to stop the count.
stopBtn.addEventListener('click', () => {
clearInterval(stopWatch);
startBtn.disabled = false;
});
// When the reset button is pressed, set the counter back to zero, then immediately update the display
resetBtn.addEventListener('click', () => {
clearInterval(stopWatch);
startBtn.disabled = false;
secondCount = 0;
displayCount();
});
// Run displayCount() once as soon as the page loads so the clock is displayed
displayCount();
</script>
</body>
</html>