index.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta
  6. name="viewport"
  7. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
  8. />
  9. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  10. <title>Светофор</title>
  11. <style>
  12. .container {
  13. padding-top: 30px;
  14. display: flex;
  15. justify-content: center;
  16. }
  17. #trafficLightBlock {
  18. background-color: black;
  19. max-width: 200px;
  20. height: 660px;
  21. padding: 30px;
  22. }
  23. #trafficLightPedestrian {
  24. margin-left: 100px;
  25. background-color: black;
  26. max-width: 200px;
  27. height: 430px;
  28. padding: 30px;
  29. }
  30. #red,
  31. #Red {
  32. width: 200px;
  33. height: 200px;
  34. border-radius: 50%;
  35. background-color: gray;
  36. margin: 0;
  37. margin-bottom: 30px;
  38. }
  39. #yellow {
  40. width: 200px;
  41. height: 200px;
  42. border-radius: 50%;
  43. background-color: gray;
  44. margin: 0;
  45. margin-bottom: 30px;
  46. }
  47. #green,
  48. #Green {
  49. width: 200px;
  50. height: 200px;
  51. border-radius: 50%;
  52. background-color: gray;
  53. margin: 0;
  54. }
  55. h1 {
  56. font-size: 80px;
  57. color: white;
  58. text-align: center;
  59. margin: 0;
  60. padding-top: 50px;
  61. }
  62. #knopka {
  63. margin-top: 50px;
  64. width: 200px;
  65. height: 50px;
  66. background-color: grey;
  67. color: white;
  68. font-size: 20px;
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <div class="container">
  74. <div id="trafficLightBlock">
  75. <div id="red">
  76. <h1 id="redTimer"></h1>
  77. </div>
  78. <div id="yellow">
  79. <h1 id="yellowTimer"></h1>
  80. </div>
  81. <div id="green">
  82. <h1 id="greenTimer"></h1>
  83. </div>
  84. </div>
  85. <div id="trafficLightPedestrian">
  86. <div id="Red">
  87. <h1 id="RedTimer"></h1>
  88. </div>
  89. <div id="Green">
  90. <h1 id="GreenTimer"></h1>
  91. </div>
  92. <button id="knopka">Click to go</button>
  93. </div>
  94. </div>
  95. <script>
  96. const delay = (ms) =>
  97. new Promise((ok) => setTimeout(() => ok(ms), ms));
  98. async function timerShow(sec, id) {
  99. for (sec; sec > 0; sec--) {
  100. id.innerHTML = sec;
  101. a = await delay(1000);
  102. id.innerHTML = "";
  103. }
  104. }
  105. async function trafficLight() {
  106. while (true) {
  107. // delay(1000).then(() => (document.getElementById("red").style.background = "red"))
  108. // delay(2000).then(() => (document.getElementById("red").style.background = "grey"))
  109. // delay(3000).then(() => (document.getElementById("yellow").style.background = "orange"))
  110. // delay(4000).then(() => (document.getElementById("yellow").style.background = "grey"))
  111. // delay(5000).then(() => (document.getElementById("green").style.background = "green"))
  112. // delay(6000).then(() => (document.getElementById("green").style.background = "grey"))
  113. let styleRed = (document.getElementById(
  114. "red"
  115. ).style.background = "red");
  116. timerShow(4, redTimer);
  117. var awaitRedGrey = await delay(5000);
  118. var styleGrey = (document.getElementById(
  119. "red"
  120. ).style.background = "grey");
  121. let styleYellow = (document.getElementById(
  122. "yellow"
  123. ).style.background = "yellow");
  124. var awaitRedGrey = await delay(2000);
  125. var styleGrey = (document.getElementById(
  126. "yellow"
  127. ).style.background = "grey");
  128. let styleGreen = (document.getElementById(
  129. "green"
  130. ).style.background = "green");
  131. timerShow(4, greenTimer);
  132. var awaitRedGrey = await delay(5000);
  133. var styleGrey = (document.getElementById(
  134. "green"
  135. ).style.background = "grey");
  136. let styleYelloww = (document.getElementById(
  137. "yellow"
  138. ).style.background = "orange");
  139. var awaitRedGrey = await delay(2000);
  140. var styleGrey = (document.getElementById(
  141. "yellow"
  142. ).style.background = "grey");
  143. }
  144. }
  145. function domEventPromise(btn, eventName) {
  146. return new Promise((resolve, reject) => {
  147. btn.addEventListener(eventName, (event) => {
  148. resolve(event);
  149. });
  150. btn.removeEventListener(eventName, (event) => {
  151. resolve(event);
  152. });
  153. });
  154. }
  155. async function pedestrianTrafficLight() {
  156. while (true) {
  157. if (true) {
  158. Red.style.background = "red";
  159. Green.style.background = "grey";
  160. knopka.disabled = true;
  161. await delay(3000);
  162. knopka.disabled = false;
  163. await Promise.race([
  164. delay(4000),
  165. domEventPromise(knopka, "click").then((e) =>
  166. console.log("event click happens", e)
  167. ),
  168. ]);
  169. }
  170. if (true) {
  171. knopka.disabled = true;
  172. Green.style.background = "green";
  173. Red.style.background = "grey";
  174. await delay(5000);
  175. }
  176. }
  177. }
  178. trafficLight();
  179. pedestrianTrafficLight();
  180. async function speedtest(getPromise, count, parallel = 1) {
  181. let duration = performance.now();
  182. let initParallel = parallel;
  183. let promisArray = [];
  184. for (let i = 0; i < count; i++) {
  185. promisArray[i] = getPromise();
  186. parallel -= 1;
  187. await Promise.all(promisArray);
  188. }
  189. console.log(promisArray);
  190. duration = performance.now() - duration;
  191. console.log(duration);
  192. console.log(initParallel * count);
  193. return {
  194. duration: duration,
  195. querySpeed: count / duration,
  196. queryDuration: duration / count,
  197. parallelSpeed: (count / duration) * initParallel,
  198. parallelDuration: duration / (initParallel * count),
  199. };
  200. }
  201. speedtest(() => delay(1000), 10, 10).then((result) =>
  202. console.log(result)
  203. );
  204. speedtest(
  205. () =>
  206. fetch("http://swapi.dev/api/people/1").then((res) =>
  207. res.json()
  208. ),
  209. 1,
  210. 2
  211. );
  212. </script>
  213. </body>
  214. </html>