-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromise_practice.html
More file actions
88 lines (78 loc) · 1.82 KB
/
promise_practice.html
File metadata and controls
88 lines (78 loc) · 1.82 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>promise_practice</title>
</head>
<body>
<div class="container">
<div id="signal"></div>
</div>
</body>
<script>
// promise 练习demo
// 红绿灯,红灯3秒,绿灯10秒,黄灯2秒
// let signal = document.getElementById('signal')
// function red_3() {
// signal.setAttribute('class', 'red')
// setTimeout(() => {
// yellow_2()
// }, 3000)
// }
// function yellow_2() {
// signal.setAttribute('class', 'yellow')
// setTimeout(() => {
// green_10()
// }, 2000)
// }
// function green_10() {
// signal.setAttribute('class', 'green')
// setTimeout(() => {
// red_3()
// }, 5000)
// }
// red_3()
function wait(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time * 1000);
})
}
async function changeColor(time, color) {
signal.setAttribute('class', color)
await wait(time)
}
async function main() {
while (true) {
await changeColor(2, 'red')
await changeColor(3, 'yellow')
await changeColor(5, 'green')
}
}
main()
</script>
<style>
.container {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center
}
#signal {
width: 100px;
height: 100px;
border-radius: 50%;
}
.green {
background: green;
}
.red {
background: red;
}
.yellow {
background: yellow;
}
</style>
</html>