index2.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  2. let dom1 = document.getElementById('color')
  3. let dom2 = document.getElementById('humanColor')
  4. let switchTraffic = true
  5. async function trafficLight(dom) {
  6. let red = document.getElementById('red')
  7. let yellow = document.getElementById('yellow')
  8. let green = document.getElementById('green')
  9. let redTimer = document.getElementById('redTimer')
  10. let greenTimer = document.getElementById('greenTimer')
  11. while (true) {
  12. await delay(500)
  13. .then(() => red.style.opacity = 1)
  14. await delay(10000)
  15. .then(() => red.style.opacity = 0.3)
  16. .then(timer(10, redTimer))
  17. await delay(500)
  18. .then(() => yellow.style.opacity = 1)
  19. await delay(1000)
  20. .then(() => yellow.style.opacity = 0.3)
  21. await delay(500)
  22. .then(() => green.style.opacity = 1)
  23. await delay(10000)
  24. .then(() => green.style.opacity = 0.3)
  25. .then(timer(10, greenTimer))
  26. await delay(500)
  27. .then(() => yellow.style.opacity = 1)
  28. await delay(1000)
  29. .then(() => yellow.style.opacity = 0.3)
  30. }
  31. }
  32. let btn = document.getElementById('btn')
  33. async function humanTrafficLight(dom) {
  34. //добавил обертку в html, указал параметр ради задания
  35. let red = document.getElementById('humanRed')
  36. let green = document.getElementById('humanGreen')
  37. let redTimer = document.getElementById('humanRedTimer')
  38. let greenTimer = document.getElementById('humanGreenTimer')
  39. if (switchTraffic) {
  40. while (true) {
  41. await delay(500)
  42. .then(() => green.style.opacity = 1)
  43. await delay(10000)
  44. .then(() => green.style.opacity = 0.3)
  45. .then(timer(10, greenTimer))
  46. await delay(1500)
  47. await delay(500)
  48. .then(() => red.style.opacity = 1)
  49. await delay(10000)
  50. .then(() => red.style.opacity = 0.3)
  51. .then(timer(10, redTimer))
  52. await delay(1500)
  53. }
  54. }
  55. else {
  56. while (true) {
  57. await delay(500)
  58. .then(() => red.style.opacity = 1)
  59. await delay(10000)
  60. .then(() => red.style.opacity = 0.3)
  61. .then(timer(10, redTimer))
  62. await delay(1500)
  63. await delay(500)
  64. .then(() => green.style.opacity = 1)
  65. await delay(10000)
  66. .then(() => green.style.opacity = 0.3)
  67. .then(timer(10, greenTimer))
  68. await delay(1500)
  69. }
  70. }
  71. }
  72. async function timer(time, value) {
  73. for (time; time > 0; time--) {
  74. value.innerHTML = time
  75. await delay(1000)
  76. value.innerHTML = ""
  77. }
  78. }
  79. trafficLight(dom1)
  80. humanTrafficLight(dom2)
  81. function domEventPromise(click, eventName) {
  82. return new Promise((resolve) => {
  83. click.addEventListener(eventName, (e) => {
  84. switchTraffic = !switchTraffic
  85. humanTrafficLight(dom2)
  86. resolve(e)
  87. })
  88. click.removeEventListener(eventName, (e) => {
  89. resolve(e)
  90. })
  91. })
  92. }
  93. btn.onclick = () => domEventPromise(btn, 'click').then(e => console.log('event click happens', e))