123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <!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>Document</title>
- <style>
- #traffic-light-body {
- border: 2px solid black;
- padding: 20px;
- border-radius: 50px;
- width: min-content;
- }
- #color-red,
- #color-yellow,
- #color-green {
- width: 100px;
- height: 100px;
- border-radius: 50%;
- border: 10px solid black;
- margin-bottom: 10px;
- background-color: transparent;
- }
- </style>
- </head>
- <body>
- <div id="traffic-light">
- <div id="color-red">
- </div>
- <div id="color-yellow">
- </div>
- <div id="color-green">
- </div>
- </div>
- </body>
- <script>
- const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
- async function trafficLight(timer) {
- while (true) {
- const red = document.getElementById('color-red')
- red.style.backgroundColor = 'red'
- await delay(timer)
- const yellow = document.getElementById('color-yellow')
- yellow.style.backgroundColor = 'yellow'
- await delay(timer)
- red.style.backgroundColor = yellow.style.backgroundColor = 'transparent'
- const green = document.getElementById('color-green')
- green.style.backgroundColor = 'green'
- await delay(timer)
- green.style.backgroundColor = 'transparent'
- }
- }
- trafficLight(1500)
- </script>
- </html>
|