main.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* function green(){
  2. console.log('green light');
  3. let div = document.getElementById('green');
  4. div.style.backgroundColor = 'green'
  5. }
  6. function yellow(){
  7. console.log('yellow light');
  8. let div = document.getElementById('yellow');
  9. div.style.backgroundColor = 'yellow'
  10. }
  11. function red(){
  12. console.log('red light');
  13. let div = document.getElementById('red');
  14. div.style.backgroundColor = 'red';
  15. } */
  16. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  17. /*
  18. async function trafficLight(parent,greenTime,yellowTime,redTime){
  19. for(let i=0; i<3;i++){
  20. let div = document.createElement('div')
  21. div.classList = 'round';
  22. parent.append(div);
  23. }
  24. while (true){
  25. // включаем зеленый
  26. parent.children[0].style.backgroundColor = 'green';
  27. parent.children[2].style.backgroundColor = '';
  28. await delay(greenTime)
  29. // включаем желтый
  30. parent.children[0].style.backgroundColor = '';
  31. parent.children[1].style.backgroundColor = 'yellow';
  32. await delay(yellowTime)
  33. // включаем красный
  34. parent.children[1].style.backgroundColor = '';
  35. parent.children[2].style.backgroundColor = 'red';
  36. await delay(redTime)
  37. }
  38. }
  39. trafficLight(trafficlight,4000,3000,4000); */
  40. function domEventPromise(parent,action){
  41. return new Promise((resolved,reject) =>{
  42. function clicked(event){
  43. resolved(event,
  44. console.log(event))
  45. parent.removeEventListener(action,clicked);
  46. }
  47. parent.addEventListener(action, clicked)}
  48. )
  49. /* const result = (event) => {
  50. console.log(event.detail)
  51. resolved(event.target)
  52. parent.removeEventListener(action,result);
  53. }
  54. parent.addEventListener(action,result);
  55. }) */
  56. }
  57. domEventPromise(knopka,'click').then((e) => console.log('event click happens', e));
  58. //pedestrian light
  59. async function pedestrianLight(parent,btn,greenTime,redTime){
  60. for(let i=0; i<2;i++){
  61. let div = document.createElement('div')
  62. div.classList = 'round';
  63. parent.append(div);
  64. }
  65. /* let btnRes = (btn) => new Promise((resolved,reject)=>{
  66. console.log(`i'm disabled`);
  67. resolved(setTimeout(btn.disabled = true, 10000))
  68. }) */
  69. let buttonResolver = (knopka) => new Promise(() => {
  70. setTimeout(() => {
  71. // console.log('start');
  72. knopka.disabled = ""}, 10000);
  73. }, () => reject)
  74. while (true){
  75. // включаем зеленый
  76. parent.children[0].style.backgroundColor = 'green';
  77. parent.children[1].style.backgroundColor = '';
  78. await /* delay(greenTime) */ Promise.race([delay(greenTime),buttonResolver(btn)])
  79. // включаем красный
  80. parent.children[0].style.backgroundColor = '';
  81. parent.children[1].style.backgroundColor = 'red';
  82. await /* delay(redTime) */ Promise.race([delay(redTime),domEventPromise(knopka,'click')])
  83. }
  84. }
  85. pedestrianLight(trafficlight,knopka,15000,10000)
  86. async function speedtest(getPromise, count,parallel=1){
  87. let duration = performance.now();
  88. let initedParalel = parallel;
  89. let promiseArr = [];
  90. for(let i = 0; i < count; i++){
  91. promiseArr[i] = getPromise();
  92. parallel-=1;
  93. await Promise.all(promiseArr);
  94. }
  95. console.log(promiseArr);
  96. duration = performance.now() - duration
  97. console.log(initedParalel*count);
  98. return {
  99. duration:duration,
  100. querySpeed: count/duration, //средняя скорость одного запроса
  101. queryDuration:duration/count, //
  102. parallelSpeed: count/duration*initedParalel,
  103. parallelDuration: duration/(initedParalel*count)
  104. }
  105. }
  106. speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result))
  107. // {duration: 10000,
  108. // querySpeed: 0.001, //1 тысячная запроса за миллисекунду
  109. // queryDuration: 1000, //1000 миллисекунд на один реальный запрос в среднем
  110. // parallelSpeed: 0.01 // 100 запросов за 10000 миллисекунд
  111. // parallelDuration: 100 // 100 запросов за 10000 миллисекунд
  112. /* speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 1, 2) */