index.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Svetophor</title>
  6. </head>
  7. <body>
  8. <style>
  9. .wrapper{
  10. background:#333;
  11. width: 150px;
  12. height: 440px;
  13. border-radius: 10px;
  14. font-size: 70px;
  15. text-align:center;
  16. padding: 1px 0;
  17. border: 6px solid #000;
  18. margin: auto;
  19. }
  20. .wrapper2 {
  21. background:#333;
  22. width: 150px;
  23. height: 299px;
  24. border-radius: 10px;
  25. font-size: 70px;
  26. text-align:center;
  27. padding: 1px 0;
  28. border: 6px solid #000;
  29. margin: auto;
  30. }
  31. #circle1, #circle2, #circle3, #cir1, #cir2{
  32. background:grey;
  33. width: 120px;
  34. height: 120px;
  35. border-radius: 50%;
  36. margin-left: 15px;
  37. margin-top: 20px;
  38. line-height: 120px;
  39. }
  40. #btn{
  41. border-radius: 50%;
  42. width: 40px;
  43. height: 40px;
  44. margin-left: 47%;
  45. margin-top: 15px;
  46. margin-bottom: 100px;
  47. }
  48. #btn:hover{
  49. opacity: 0.6;
  50. }
  51. p{
  52. margin: 60px;
  53. }
  54. </style>
  55. <div class="wrapper">
  56. <div id="circle1">
  57. </div>
  58. <div id="circle2">
  59. </div>
  60. <div id="circle3">
  61. </div>
  62. </div>
  63. <p></p>
  64. <div class="wrapper2">
  65. <div id="cir1">
  66. </div>
  67. <div id="cir2">
  68. </div>
  69. </div>
  70. <input type="submit" id="btn" value="&#9731;" />
  71. <script>
  72. //----------------------------светофор
  73. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  74. function timer(elem,ms){
  75. let count=ms;
  76. let number=document.createElement('span');
  77. number.innerHTML=count;
  78. number.style.color='blue';
  79. elem.append(number);
  80. let h=setInterval(()=>{
  81. number.innerHTML=--count;
  82. if(count==0){
  83. clearInterval(h);
  84. number.remove();
  85. }
  86. },1000);
  87. }
  88. function g1(){
  89. timer(circle3,6);
  90. circle3.style.background='green';
  91. }
  92. function g2(){
  93. circle3.style.background='grey';
  94. }
  95. function y1(){
  96. timer(circle2,2);
  97. circle2.style.background='yellow';
  98. }
  99. function y2(){
  100. circle2.style.background='grey';
  101. }
  102. function r1(){
  103. timer(circle1,10);
  104. circle1.style.background='red';
  105. }
  106. function r2(){
  107. circle1.style.background='grey';
  108. }
  109. async function trafficLight(delayGreen,delayYellow,delayRed){
  110. while (true){
  111. y2()
  112. g1()
  113. await delay(delayGreen)
  114. g2()
  115. y1()
  116. await delay(delayYellow)
  117. y2()
  118. r1()
  119. await delay(delayRed)
  120. r2()
  121. y1()
  122. await delay(delayYellow)
  123. }
  124. }
  125. (async()=>{
  126. await trafficLight(6000,2000,10000);
  127. })();
  128. //----------------------domEventPromise
  129. async function domEventPromise(knopka,event){
  130. return new Promise((resolve,reject)=>{
  131. try{
  132. knopka.addEventListener(event,async(e)=>{
  133. resolve(e);
  134. knopka.removeEventListener(event,this);
  135. })
  136. }catch(e){
  137. reject(e);
  138. }
  139. })
  140. }
  141. //domEventPromise(btn, 'click').then(e => console.log('event click happens', e));
  142. //--------------PedestrianTrafficLight
  143. async function PedestrianTrafficLight(){
  144. while(true){
  145. cir1.style.backgroundColor = 'red';
  146. await Promise.race([
  147. domEventPromise(btn, 'click').then(async (e) => {
  148. cir1.style.backgroundColor = 'grey';
  149. cir2.style.backgroundColor = 'green';
  150. timer(cir2,4);
  151. await delay(4000);
  152. cir2.style.backgroundColor = 'grey';
  153. cir1.style.backgroundColor = 'red';
  154. timer(cir1,7);
  155. }, err => console.log(err))
  156. ]);
  157. await delay(7000);
  158. }
  159. }
  160. (async () => await PedestrianTrafficLight())();
  161. //-----------speedTest
  162. async function speedTest(getPromise, count, parallel=1){
  163. let duration, querySpeed, queryDuration, parallelSpeed, parallelDuration;
  164. let start = performance.now();
  165. let arr = [];
  166. await (async () => {
  167. for (let i=0;i<count;i++) {
  168. let arrPromise = []
  169. for (let j=0;j<parallel;j++) {
  170. arrPromise.push(getPromise);
  171. }
  172. let querySpeedItemStar=performance.now();
  173. await Promise.all([...arrPromise.map(item => item())]);
  174. arr.push(performance.now()-querySpeedItemStart);
  175. }
  176. })()
  177. duration = Math.round(performance.now() - start);
  178. querySpeed = (count*parallel)/100000;
  179. queryDuration = arr.reduce((a,b)=>(a+b))/arr.length;
  180. parallelSpeed = duration/(count*parallel)/10000;
  181. parallelDuration = duration/(count*parallel);
  182. return{
  183. duration,
  184. querySpeed,
  185. queryDuration,
  186. parallelSpeed,
  187. parallelDuration
  188. }
  189. }
  190. speedTest(()=>delay(1000),10,10).then(result=>console.log(result));
  191. speedTest(()=>fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5)
  192. </script>
  193. </body>
  194. </html>