12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <!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 {
- border: 2px solid black;
- padding: 20px;
- border-radius: 50px;
- width: min-content;
- }
- #color-red,
- #color-green {
- width: 100px;
- height: 100px;
- border-radius: 50%;
- border: 10px solid black;
- margin-bottom: 10px;
- background-color: transparent;
- }
- #btn {
- padding: 5px 25px;
- font-size: 1.2em;
- min-width: 164px;
- margin-top: 20px;
- }
- </style>
- </head>
- <body>
- <div id="traffic-light">
- <div id="color-red">
- </div>
- <div id="color-green">
- </div>
- </div>
- <button id="btn" type="button">Зеленый</button>
- </body>
- <script>
- const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
- function domEventPromise(element, nameOfEvent) {
- function executor(resolve) {
- function event(param) {
- element.disabled = true
- element.removeEventListener(nameOfEvent, event)
- }
- element.disabled = false
- element.addEventListener(nameOfEvent, event)
- }
- return new Promise(executor)
- }
- async function pedestrianTrafficLight(ms) {
- while (true) {
- const red = document.getElementById('color-red')
- red.style.backgroundColor = 'red'
- await Promise.race([
- delay(ms),
- domEventPromise(document.getElementById('btn'), 'click')
- ])
- red.style.backgroundColor = 'transparent'
- const green = document.getElementById('color-green')
- green.style.backgroundColor = 'green'
- await delay(ms)
- green.style.backgroundColor = 'transparent'
- }
- }
- pedestrianTrafficLight(6000)
- </script>
- </html>
|