script.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. let red = document.querySelector('.red'),
  2. yellow = document.querySelector('.yellow'),
  3. green = document.querySelector('.green'),
  4. btn = document.querySelector('.butn'),
  5. counter = document.querySelector('.counter');
  6. let shwCounts,timerss,startTime = true;
  7. const doo=ms=>new Promise(ok=>setTimeout(()=> ok(ms),ms));
  8. let low = ms => new Promise(function(res){
  9. btn.onclick = () => {
  10. buttonClick();
  11. clearTimeout(timerss);
  12. }
  13. timerss=setTimeout(()=>res(),ms);
  14. function buttonClick(){
  15. setTimeout(() => res(),4000);
  16. };
  17. });
  18. let redTimer = 6000,
  19. yellowTimer = 2000,
  20. greenTimer = 8000;
  21. async function trafficLight(){
  22. while (true){
  23. shw(parseInt(redTimer));
  24. yellow.classList.remove('burn-yellow');
  25. red.classList.add('burn-red');
  26. await doo(redTimer);
  27. shw(parseInt(yellowTimer));
  28. red.classList.remove('burn-red');
  29. yellow.classList.add('burn-yellow');
  30. await doo(yellowTimer);
  31. shw(parseInt(greenTimer));
  32. yellow.classList.remove('burn-yellow');
  33. green.classList.add('burn-green');
  34. await Promise.race([doo(greenTimer),low(greenTimer+1000)]);
  35. clearTimeout(shwCounts);
  36. counter.innerText = parseInt(yellowTimer/1000);
  37. shw(parseInt(yellowTimer));
  38. green.classList.remove('burn-green');
  39. yellow.classList.add('burn-yellow');
  40. await doo(yellowTimer);
  41. };
  42. };
  43. trafficLight();
  44. function shw(time) {
  45. if (startTime){
  46. counter.innerText = time/1000;
  47. startTime = false
  48. }
  49. time -= 1000;
  50. if (time == 0){
  51. startTime = true;
  52. return;
  53. }
  54. shwCounts = setTimeout(()=>{
  55. counter.innerText = time/1000;
  56. shw(time);
  57. },1000);
  58. }
  59. async function speedTest(promis,cnt=1,paraller=1){
  60. let prll = paraller, obj = {}, fdr = 0;;
  61. function prllStart(){
  62. if (paraller === 1){
  63. paraller = prll;
  64. return(promis());
  65. }else{
  66. paraller--;
  67. prllStart();
  68. return(promis());
  69. }
  70. }
  71. let run = performance.now();
  72. for (let i=0; i<cnt ; i++){
  73. let a = performance.now();
  74. await Promise.all([prllStart()]);
  75. fdr += performance.now() - a;
  76. }
  77. obj.summParaller = paraller + ' пралельных процессов'
  78. obj.lasting = performance.now() - run;
  79. obj.parallerLasting = fdr / cnt;
  80. obj.parallerSpeed = paraller / (obj.parallerLasting);
  81. obj.durationLasting = parseInt(obj.parallerLasting) + 'ms'
  82. obj.requestSpeed='нет отличия'
  83. return obj;
  84. }
  85. speedTest(()=>doo(1000),10,100).then(result=>console.log(result));
  86. speedTest(()=>fetch('http://swapi.dev/api/people/1').then(res=>res.json()),10,5).then(result=>console.log(result));