JS 倒计时定时器

JS 60秒倒计时代码

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
<!DOCTYPE html>
<html lang="en">

<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>
</head>

<style>
.main {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}

#timer {
width: 100px;
}
</style>

<body>
<div class="main">
<div id="timer"></div>
<a href="javascript:;" id="startBtn">开始</a>
</div>
<script>
let startBtn = document.getElementById('startBtn');
let timerNode = document.getElementById('timer');
let senond = 0
function timer() {
startBtn.style = "display:none";
senond = 60;
timerNode.innerText = senond;
let promise = new Promise((resolve, reject) => {
let setTimer = setInterval(() => {

senond--;
timerNode.innerText = senond;
console.log(senond);

if (senond <= 0) {
resolve(setTimer);
startBtn.style = "display:block";
}
}, 1000);
});
promise.then((setTimer) => {
clearInterval(setTimer);
});
}


startBtn.addEventListener('click', timer, false);
</script>
</body>

</html>
坚持原创技术分享,您的支持将鼓励我继续创作!
0%