script.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ///Async/Await Homework 2
  2. let carTrafficLight = document.querySelector('.carTrafficLight');
  3. let pedestrianTrafficLight = document.querySelector('.pedestrianTrafficLight');
  4. let lightsForCars = carTrafficLight.querySelectorAll('.light');
  5. let lightsForPedestrians = pedestrianTrafficLight.querySelectorAll('div');
  6. let btnTurnOn = document.querySelector('.btnTurnOn');
  7. let currentParent = []
  8. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
  9. function domEventPromise(elem, eventName){
  10. return new Promise((resolve) => {
  11. let funcResolve = function(){
  12. resolve(cleanParent);
  13. elem.removeEventListener(eventName, funcResolve);
  14. }
  15. elem.addEventListener(eventName, funcResolve);
  16. })
  17. }
  18. function cleanParent(number){
  19. currentParent[number].innerHTML = '';
  20. }
  21. async function counter(startms, finishms, parent, number){
  22. currentParent[number] = parent
  23. let span = document.createElement('span');
  24. parent.append(span);
  25. for(let i = startms/1000; i>=finishms/1000; i--){
  26. span.innerHTML = i
  27. await delay(1000)
  28. }
  29. return cleanParent
  30. }
  31. function light(elem, color){
  32. for (let e of elem){
  33. e.style.backgroundColor = ''
  34. }
  35. if (color === 'red'){
  36. elem[0].style.backgroundColor = color
  37. } else if(color === 'yellow'){
  38. elem[1].style.backgroundColor = color
  39. } else if(color === 'green'){ // у trafficLightForPedestrians тоже три div просто желтый невидим,поэтому зеленый [2]
  40. elem[2].style.backgroundColor = color
  41. }
  42. }
  43. async function trafficLightForPedestrians(){
  44. while(true){
  45. light(lightsForPedestrians,'red');
  46. await counter(25000, 17000,lightsForPedestrians[0],1).then(func => func(1)); //// задержка чтобы не наглели (c 16 cek)
  47. await Promise.race([counter(16000, 1000,lightsForPedestrians[0],1), domEventPromise(btnTurnOn, "click")]).then(func => func(1));
  48. light(lightsForPedestrians,'green');
  49. await counter(10000, 1000,lightsForPedestrians[2],1).then(func => func(1)); //func(1) или func(2) обнуление счетчика(из массива currentParent)
  50. }
  51. }
  52. async function trafficLightForCars(){
  53. while(true){
  54. light(lightsForCars,'yellow');
  55. await counter(3000, 1000,lightsForCars[1],0).then(func => func(0));
  56. light(lightsForCars,'green');
  57. await counter(22000, 17000,lightsForCars[2],0).then(func => func(0)); ///задержка чтобы не наглели на (c 16 cek)
  58. await Promise.race([counter(16000, 1000,lightsForCars[2],0),domEventPromise(btnTurnOn, "click")]).then(func => func(0));
  59. light(lightsForCars,'red');
  60. await counter(10000, 1000,lightsForCars[0],0).then(func => func(0));
  61. }
  62. }
  63. trafficLightForPedestrians()
  64. trafficLightForCars();
  65. //////////////////////////////////speedTest
  66. async function speedTest(getPromise, count, parallel=1){
  67. let createPromiseArr = function (){
  68. let arrPromise = []
  69. for (let i = 0; i<parallel; i++){
  70. arrPromise.push(getPromise())
  71. }
  72. return arrPromise
  73. }
  74. let startDuration = performance.now();
  75. for (let i = 0; i < count; i++){
  76. await Promise.all(createPromiseArr())
  77. }
  78. let endDuration = performance.now();
  79. let duration = endDuration - startDuration;
  80. let querySpeed = count /duration;
  81. let queryDuration = duration / count;
  82. let parallelSpeed = count * parallel / duration;
  83. let parallelDuration = duration / (count * parallel);
  84. return {
  85. duration,
  86. querySpeed,
  87. queryDuration,
  88. parallelSpeed,
  89. parallelDuration
  90. }
  91. }
  92. speedTest(() => delay(1000), 10, 10 ).then(result => console.log(result));