Dz14 js.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. .container {
  10. display: flex;
  11. padding: 50px 0 0 100px;
  12. }
  13. #trafficLight {
  14. max-width: 200px;
  15. height: 700px;
  16. background-color: black;
  17. }
  18. #Red {
  19. width: 200px;
  20. height: 200px;
  21. border-radius: 50%;
  22. background-color: gray;
  23. margin: 0 0 30px 0;
  24. }
  25. #Yellow {
  26. width: 200px;
  27. height: 200px;
  28. border-radius: 50%;
  29. background-color: gray;
  30. margin: 0 0 30px 0;
  31. }
  32. #Green {
  33. width: 200px;
  34. height: 200px;
  35. border-radius: 50%;
  36. background-color: gray;
  37. margin: 0;
  38. }
  39. h3 {
  40. padding: 90px 0 0 95px;
  41. }
  42. #pedestrianTrafficLight {
  43. max-width: 200px;
  44. height: 460px;
  45. background-color: black;
  46. margin-left: 100px;
  47. }
  48. #red {
  49. width: 200px;
  50. height: 200px;
  51. border-radius: 50%;
  52. background-color: gray;
  53. }
  54. #green {
  55. width: 200px;
  56. height: 200px;
  57. border-radius: 50%;
  58. background-color: gray;
  59. }
  60. button {
  61. width: 150px;
  62. height: 30px;
  63. margin: 20px 0 0 420px;
  64. display: inline;
  65. }
  66. </style>
  67. </head>
  68. <body>
  69. <div class="container">
  70. <div id="trafficLight">
  71. <div id="Red">
  72. <h3 id="timerRed"></h3>
  73. </div>
  74. <div id="Yellow">
  75. <h3 id="timerYellow"></h3>
  76. </div>
  77. <div id="Green">
  78. <h3 id="timerGreen"></h3>
  79. </div>
  80. </div>
  81. <div id="pedestrianTrafficLight">
  82. <div id="red">
  83. <h3 id="redTimer"></h3>
  84. </div>
  85. <div id="green">
  86. <h3 id="greenTimer"></h3>
  87. </div>
  88. </div>
  89. </div>
  90. <button id="knopka">Сhange</button>
  91. <script>
  92. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  93. async function timer(time, id) {
  94. for (time; time >= 0; time--) {
  95. id.innerHTML = time
  96. sec = await delay(1000)
  97. id.innerHTML = ''
  98. }
  99. }
  100. function domEventPromise(button, eventName) {
  101. return new Promise((resolve, reject) => {
  102. button.addEventListener(eventName, (e) => {
  103. resolve(e);
  104. });
  105. button.removeEventListener(eventName, (e) => {
  106. resolve(e);
  107. });
  108. });
  109. }
  110. domEventPromise(knopka, 'click').then(e => console.log('event click happens', e))
  111. async function trafficLight() {
  112. while (true) {
  113. if (true) {
  114. Green.style.background = "#00FF00";
  115. Red.style.background = "grey";
  116. knopka.disabled = true
  117. await delay(3000);
  118. knopka.disabled = false
  119. await Promise.race([
  120. delay(5000),
  121. domEventPromise(knopka, 'click').then((e) => console.log('event click happens', e))
  122. ])
  123. }
  124. var styleYellow = (document.getElementById("Yellow").style.background = "#FFD700");
  125. var awaitGrey = await delay(1000);
  126. var styleGrey = (document.getElementById("Yellow").style.background = "grey");
  127. if (true) {
  128. Red.style.background = "#FF0000";
  129. Green.style.background = "grey";
  130. knopka.disabled = true
  131. await delay(3000);
  132. knopka.disabled = false
  133. await Promise.race([
  134. delay(5000),
  135. ])
  136. }
  137. var styleYellow = (document.getElementById("Yellow").style.background = "#FFD700");
  138. var awaitGrey = await delay(2000);
  139. var styleGrey = (document.getElementById("Yellow").style.background = "grey");
  140. }
  141. // var styleYellow = (document.getElementById("Yellow").style.background = "#FFD700");
  142. // var awaitGrey = await delay(2000);
  143. // var styleGrey = (document.getElementById("Yellow").style.background = "grey");
  144. // var styleYeGreen = (document.getElementById("Green").style.background = "#00FF00");
  145. // var awaitGrey = await delay(6000);
  146. // var styleGrey = (document.getElementById("Green").style.background = "grey");
  147. // var styleYellow = (document.getElementById("Yellow").style.background = "#FFD700");
  148. // var awaitGrey = await delay(2000);
  149. // var styleGrey = (document.getElementById("Yellow").style.background = "grey");
  150. }
  151. // }
  152. trafficLight()
  153. // async function trafficLight() {
  154. // while (true) {
  155. // var styleRed = (document.getElementById("Red").style.background = "#FF0000");
  156. // timer(5, timerRed)
  157. // var awaitGrey = await delay(6000);
  158. // var styleGrey = (document.getElementById("Red").style.background = "grey");
  159. // var styleYellow = (document.getElementById("Yellow").style.background = "#FFD700");
  160. // timer(1, timerYellow)
  161. // var awaitGrey = await delay(2000);
  162. // var styleGrey = (document.getElementById("Yellow").style.background = "grey");
  163. // var styleYeGreen = (document.getElementById("Green").style.background = "#00FF00");
  164. // timer(5, timerGreen)
  165. // var awaitGrey = await delay(6000);
  166. // var styleGrey = (document.getElementById("Green").style.background = "grey");
  167. // var styleYellow = (document.getElementById("Yellow").style.background = "#FFD700");
  168. // timer(1, timerYellow)
  169. // var awaitGrey = await delay(2000);
  170. // var styleGrey = (document.getElementById("Yellow").style.background = "grey");
  171. // }
  172. // }
  173. // trafficLight()
  174. async function pedestrianTrafficLight() {
  175. while (true) {
  176. if (true) {
  177. red.style.background = "#FF0000";
  178. green.style.background = "grey";
  179. knopka.disabled = true
  180. await delay(3000);
  181. knopka.disabled = false;
  182. await Promise.race([
  183. delay(5000),
  184. domEventPromise(knopka, 'click').then((e) => {
  185. console.log('event click happens', e)
  186. })
  187. ])
  188. if (true) {
  189. await delay(1000)
  190. knopka.disabled = true
  191. green.style.background = " #00FF00"
  192. red.style.background = "grey"
  193. await delay(8000)
  194. red.style.background ="grey"
  195. green.style.background = "grey"
  196. await delay(2000)
  197. }
  198. }
  199. }
  200. }
  201. pedestrianTrafficLight()
  202. async function speedtest(getPromise, count, parallel = 1) {
  203. let duration = performance.now();
  204. let initParallel = parallel;
  205. let promisArray = [];
  206. for (let i = 0; i < count; i++) {
  207. promisArray[i] = getPromise();
  208. parallel -= 1;
  209. await Promise.all(promisArray);
  210. }
  211. console.log(promisArray);
  212. duration = performance.now() - duration;
  213. console.log(duration);
  214. console.log(initParallel * count);
  215. return {
  216. duration: duration,
  217. querySpeed: count / duration,
  218. queryDuration: duration / count,
  219. parallelSpeed: (count / duration) * initParallel,
  220. parallelDuration: duration / (initParallel * count),
  221. };
  222. }
  223. speedtest(() => delay(1000), 10, 10).then((result) =>
  224. console.log(result)
  225. );
  226. speedtest(
  227. () =>
  228. fetch("http://swapi.dev/api/people/1").then((res) =>
  229. res.json()
  230. ),
  231. 1,
  232. 2
  233. );
  234. </script>
  235. </body>
  236. </html>