index.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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>Document</title>
  8. <style>
  9. .circle_gray {
  10. width: 100px;
  11. height: 100px;
  12. background-color: gray;
  13. border-radius: 50%;
  14. }
  15. .circle_green {
  16. width: 100px;
  17. height: 100px;
  18. background-color: green;
  19. border-radius: 50%;
  20. }
  21. .circle_yellow {
  22. width: 100px;
  23. height: 100px;
  24. background-color: yellow;
  25. border-radius: 50%;
  26. }
  27. .circle_red {
  28. width: 100px;
  29. height: 100px;
  30. background-color: red;
  31. border-radius: 50%;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="divTrafficLight"></div>
  37. <script>
  38. let divContainer = document.createElement('div')
  39. //trafficLight
  40. let divTraffic = document.createElement('div')
  41. divTraffic.setAttribute("style", "background-color: black; display: inline-block; margin-right: 200px;")
  42. let green = document.createElement('div')
  43. let yellow = document.createElement('div')
  44. let red = document.createElement('div')
  45. green.classList.add('circle_gray')
  46. yellow.classList.add('circle_gray')
  47. red.classList.add('circle_gray')
  48. divTraffic.appendChild(green)
  49. divTraffic.appendChild(yellow)
  50. divTraffic.appendChild(red)
  51. divContainer.appendChild(divTraffic)
  52. //pedestrian trafficLight
  53. let divPedTraffic = document.createElement('div')
  54. divPedTraffic.setAttribute("style", "background-color: black; display: inline-block; margin-left: 200px; position: relative;")
  55. let knopka = document.createElement('button')
  56. knopka.innerText = "Press"
  57. knopka.setAttribute("style", "position: absolute; left: 30%;")
  58. let greenPed = document.createElement('div')
  59. let redPed = document.createElement('div')
  60. greenPed.classList.add('circle_gray')
  61. redPed.classList.add('circle_gray')
  62. divPedTraffic.appendChild(greenPed)
  63. divPedTraffic.appendChild(redPed)
  64. divPedTraffic.appendChild(knopka)
  65. divContainer.appendChild(divPedTraffic)
  66. divTrafficLight.appendChild(divContainer)
  67. let timeouts = [];
  68. const delay = ms => new Promise(ok => {
  69. let timeoutStack = setTimeout(() => ok(ms), ms)
  70. timeouts.push(timeoutStack)
  71. })
  72. async function trafficLight(gMs, yMs, rMs) {
  73. while (true) {
  74. await Promise.race([domEventPromise(knopka, 'click', 0), delay(yMs)])
  75. //yellow
  76. green.classList.remove('circle_green')
  77. yellow.classList.add('circle_yellow')
  78. await delay(yMs)
  79. //red
  80. yellow.classList.remove('circle_yellow')
  81. red.classList.add('circle_red')
  82. //ped
  83. redPed.classList.remove('circle_red')
  84. greenPed.classList.add('circle_green')
  85. knopka.disabled = true
  86. await delay(rMs)
  87. //yellow
  88. red.classList.remove('circle_red')
  89. yellow.classList.add('circle_yellow')
  90. await delay(yMs)
  91. //green
  92. yellow.classList.remove('circle_yellow')
  93. green.classList.add('circle_green')
  94. //ped
  95. greenPed.classList.remove('circle_green')
  96. redPed.classList.add('circle_red')
  97. setTimeout(() => knopka.disabled = false, rMs / 3)
  98. await delay(gMs)
  99. }
  100. }
  101. let resetTrafficLights = () => {
  102. timeouts.forEach((timeoutStack) => {
  103. clearTimeout(timeoutStack)
  104. })
  105. }
  106. let buttonClick = async () => {
  107. resetTrafficLights();
  108. trafficLight(10000, 2000, 10000);
  109. }
  110. //domEventPromise
  111. function domEventPromise(elem, event) {
  112. return new Promise((resolve, reject) => {
  113. let handler = async (event) => {
  114. buttonClick()
  115. resolve(event)
  116. elem.removeEventListener(event, handler)
  117. }
  118. elem.addEventListener(event, handler);
  119. })
  120. }
  121. trafficLight(10000, 2000, 10000)
  122. //speedtest1
  123. // async function speedtest(getPromise, count, parallel = 1) {
  124. // async function handler() {
  125. // return getPromise
  126. // }
  127. // return {
  128. // duration,
  129. // querySpeed, //средняя скорость одного запроса
  130. // queryDuration, //
  131. // parallelSpeed,
  132. // parallelDuration
  133. // }
  134. // }
  135. // speedtest(() => delay(1000), 10, 10).then(result => console.log(result))
  136. // // {duration: 10000,
  137. // // querySpeed: 0.001, //1 тысячная запроса за миллисекунду
  138. // // queryDuration: 1000, //1000 миллисекунд на один реальный запрос в среднем
  139. // // parallelSpeed: 0.01 // 100 запросов за 10000 миллисекунд
  140. // // parallelDuration: 100 // 100 запросов за 10000 миллисекунд
  141. // speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5)
  142. </script>
  143. </body>
  144. </html>