-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsetTimeout返回数据.html
99 lines (85 loc) · 2.63 KB
/
setTimeout返回数据.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>setTimeout</title>
<link rel="stylesheet" href="">
</head>
<body>
<div>
<h3>倒计时一:</h3>
<p class="timer" data-leftTime="20"></p>
</div>
<div>
<h3>倒计时二:</h3>
<p class="timer" data-leftTime="10"></p>
</div>
<div>
<h3>倒计时三:</h3>
<p class="timer" data-leftTime="60"></p>
</div>
<script type="text/javascript">
// 多个定时器
let pkNodeList = document.querySelectorAll(".timer")
console.log(pkNodeList)
let allTimerIns = {}
// for (let i = 0; i < pkNodeList.length; i++) {
// countDown(allTimerIns[pkNodeList[i]], pkNodeList[i], parseInt(pkNodeList[i].getAttribute('data-leftTime')))
// }
pkNodeList.forEach(item => {
console.log(item)
countDown(allTimerIns[item], item, parseInt(item.getAttribute('data-leftTime')))
})
// console.log(allTimerIns)
function countDown (currentTimerIns, element, time) {
// debugger
console.log(currentTimerIns, element, time)
// currentTimerIns && clearTimeout(currentTimerIns)
let minutes = Math.floor(time / 60)
let seconds = time % 60
minutes = minutes < 10 ? '0' + minutes : minutes
seconds = seconds < 10 ? '0' + seconds : seconds
element.innerHTML = `${minutes}:${seconds}`
element.setAttribute("data-leftTime", time)
if (time <= 0) {
clearTimeout(currentTimerIns)
currentTimerIns = null
return
}
currentTimerIns = setTimeout(() => {
time--
countDown(currentTimerIns, element, time)
}, 1000)
}
// 一个定时器
/*let timerIns = null
let time = document.querySelector(".timer").getAttribute('data-leftTime')
time = parseInt(time)
console.log(time)
function startPkCountDown (time) {
timerIns && clearTimeout(timerIns)
if (time <= 0) {
timerIns = null
return
}
let minutes = Math.floor(time / 60)
let seconds = time % 60
minutes = minutes < 10 ? '0' + minutes : minutes
seconds = seconds < 10 ? '0' + seconds : seconds
document.querySelector(".timer").innerHTML = `${minutes}:${seconds}`
document.querySelector(".timer").setAttribute("data-leftTime", time)
timerIns = setTimeout(() => {
time--
startPkCountDown(time)
}, 1000)
console.log(timerIns)
}
startPkCountDown(time)
setTimeout(() => {
time = 10
startPkCountDown(time)
}, 5000)*/
</script>
</body>
</html>