index.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. </head>
  9. <style>
  10. body{
  11. display: flex;
  12. }
  13. .light {
  14. width:105px;
  15. height: 405px;
  16. padding: 10px;
  17. border-radius: 15%;
  18. display: flex;
  19. flex-direction: column;
  20. margin-top: 150px;
  21. background-color: black;
  22. /* transform: rotate(-90deg); */
  23. }
  24. .light2{
  25. width:105px;
  26. height: 405px;
  27. padding: 10px;
  28. margin: 30px;
  29. border-radius: 15%;
  30. display: flex;
  31. flex-direction: column;
  32. margin-top: 150px;
  33. background-color: black;
  34. }
  35. .green{
  36. width: 100px;
  37. height: 100px;
  38. border-radius: 50%;
  39. background-color: green;
  40. visibility: hidden;
  41. }
  42. .yellow{
  43. width: 100px;
  44. height: 100px;
  45. border-radius: 50%;
  46. background-color: yellow;
  47. visibility: hidden;
  48. }
  49. .red{
  50. width: 100px;
  51. height: 100px;
  52. border-radius: 50%;
  53. background-color: red;
  54. visibility: visible;
  55. }
  56. .knopka {
  57. background-color: coral;
  58. border-radius: 50%;
  59. width: 100px;
  60. height: 100px;
  61. display: flex;
  62. text-transform: uppercase;
  63. justify-content: center;
  64. align-items: center;
  65. border: 2px solid black;
  66. }
  67. </style>
  68. <body>
  69. <div id="light" class="light">
  70. <div class="red" id="red"></div>
  71. <div class="yellow" id="yellow"></div>
  72. <div class="green" id="green"></div>
  73. </div>
  74. <div id="light" class="light2">
  75. <div class="red" id="red1"></div>
  76. <div class="yellow" id="yellow1"></div>
  77. <div class="green" id="green1"></div>
  78. <div class="knopka" id="knopka">Натисніть</div>
  79. </div>
  80. <script>
  81. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  82. async function trafficLight(dom){
  83. dom = document.getElementById('light')
  84. let green = document.getElementById('green')
  85. let red = document.getElementById('red')
  86. let yellow = document.getElementById('yellow')
  87. while (true){
  88. green.style.visibility = 'visible'
  89. red.style.visibility = 'hidden'
  90. await delay(2000)
  91. yellow.style.visibility = 'visible'
  92. green.style.visibility = 'hidden'
  93. await delay(2000)
  94. red.style.visibility = 'visible'
  95. green.style.visibility = 'hidden'
  96. yellow.style.visibility = 'hidden'
  97. await delay(2000)
  98. }
  99. }
  100. trafficLight();
  101. //PedestrianTrafficLight
  102. async function pedestrianTrafficLight(dom){
  103. dom = document.getElementById('light')
  104. let green = document.getElementById('green1')
  105. let red = document.getElementById('red1')
  106. let yellow = document.getElementById('yellow1')
  107. let timerRed = setTimeout(() => {
  108. red.style.visibility = 'visible'
  109. }, 500)
  110. let timerYellow = setTimeout(() => {
  111. red.style.visibility = 'hidden'
  112. yellow.style.visibility = 'visible'
  113. }, 1500)
  114. let timerGreen = setTimeout(() => {
  115. yellow.style.visibility = 'hidden'
  116. green.style.visibility = 'visible'
  117. }, 2500)
  118. setTimeout(() => { clearInterval(timerRed); red.style.visibility = 'visible' }, 5050);
  119. setTimeout(() => { clearInterval(timerYellow); yellow.style.visibility = 'hidden'}, 5000);
  120. setTimeout(() => { clearInterval(timerGreen);green.style.visibility = 'hidden' }, 5000);
  121. }
  122. //domEventPromise
  123. function domEventPromise(element , eventName){
  124. return new Promise((resolve , reject) => {
  125. element.addEventListener(eventName , (e) => {
  126. resolve(e);
  127. console.log('event click happens')
  128. pedestrianTrafficLight(e)
  129. })
  130. })
  131. }
  132. domEventPromise(knopka, 'click')
  133. // speedtest
  134. async function speedtest(getPromise, count, parallel = 1) {
  135. let duration = 0;
  136. for (let i = 0; i < count; i++) {
  137. let allPromises = [];
  138. let start = performance.now();
  139. for (let index = 1; index <= parallel; index++) {
  140. allPromises[allPromises.length] = getPromise();
  141. }
  142. await Promise.all(allPromises);
  143. let end = performance.now();
  144. duration += Math.ceil(end - start);
  145. }
  146. let querySpeed = +(count / duration).toFixed(5)
  147. let queryDuration = Math.ceil((duration / parallel) - 3)
  148. let parallelSpeed = +((count * parallel) / duration).toFixed(2)
  149. let parallelDuration = Math.ceil(duration / (count * parallel) - 1)
  150. return {
  151. duration,
  152. querySpeed,
  153. queryDuration,
  154. parallelSpeed,
  155. parallelDuration,
  156. };
  157. }
  158. speedtest(() => delay(1000), 10, 10).then((result) => console.log(result));
  159. // {duration: 10000,
  160. // querySpeed: 0.001, //1 тысячная запроса за миллисекунду
  161. // queryDuration: 1000, //1000 миллисекунд на один реальный запрос в среднем
  162. // parallelSpeed: 0.01 // 100 запросов за 10000 миллисекунд
  163. // parallelDuration: 100 // 100 запросов за 10000 миллисекунд
  164. speedtest(() => fetch("http://swapi.dev/api/people/1").then((res) => res.json()), 10, 5).then((result) =>
  165. console.log(result)
  166. );
  167. </script>
  168. </body>
  169. </html>