Светофор для пешеходов.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Светофор для пешеходов</title>
  8. </head>
  9. <body>
  10. <main id="main" style="width: max-content;">Светофор для пешеходов</main>
  11. <script>
  12. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  13. let divGreen = document.createElement('div')
  14. let divRed = document.createElement('div')
  15. divGreen.innerText='Зеленый'
  16. divRed.innerText='Красный'
  17. let greenOn =()=> divGreen.style="background-color: rgb(0, 216, 0) ;"
  18. let greenOf =()=> divGreen.style="background-color: rgb(12, 105, 12) ;"
  19. let redOn =()=> divRed.style="background-color: rgb(216, 0, 0) ;"
  20. let redOf =()=> divRed.style="background-color: rgb(139, 7, 7);"
  21. let animation = true
  22. async function trafficLight(timeGreen,timeRed){
  23. greenOf()
  24. redOf()
  25. while (animation){
  26. redOf()
  27. greenOn()
  28. await delay(timeGreen)
  29. greenOf()
  30. redOn()
  31. await delay(timeRed)
  32. }
  33. }
  34. trafficLight(5000,6000)
  35. let knopka = document.createElement('button')
  36. knopka.innerText='Кнопка'
  37. document.body.append(knopka)
  38. function domEventPromise(element, eventName) {
  39. function executor(resolve) {
  40. async function handler(event) {
  41. animation=false
  42. redOf()
  43. greenOn()
  44. await delay(5000)
  45. greenOf()
  46. animation=true
  47. trafficLight(5000,6000)
  48. resolve(event);
  49. }
  50. element.addEventListener(eventName, handler);
  51. }
  52. return new Promise(executor);
  53. }
  54. domEventPromise(knopka, 'click').then(e => e);
  55. document.getElementById('main').append(divGreen,divRed)
  56. </script>
  57. </body>
  58. </html>