123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- ///Async/Await Homework 2
- let carTrafficLight = document.querySelector('.carTrafficLight');
- let pedestrianTrafficLight = document.querySelector('.pedestrianTrafficLight');
- let lightsForCars = carTrafficLight.querySelectorAll('.light');
- let lightsForPedestrians = pedestrianTrafficLight.querySelectorAll('div');
- let btnTurnOn = document.querySelector('.btnTurnOn');
- let currentParent = []
- const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
- function domEventPromise(elem, eventName){
- return new Promise((resolve) => {
- let funcResolve = function(){
- resolve(cleanParent);
- elem.removeEventListener(eventName, funcResolve);
- }
- elem.addEventListener(eventName, funcResolve);
- })
- }
- function cleanParent(number){
- currentParent[number].innerHTML = '';
- }
- async function counter(startms, finishms, parent, number){
- currentParent[number] = parent
- let span = document.createElement('span');
- parent.append(span);
- for(let i = startms/1000; i>=finishms/1000; i--){
- span.innerHTML = i
- await delay(1000)
- }
- return cleanParent
- }
- function light(elem, color){
- for (let e of elem){
- e.style.backgroundColor = ''
- }
- if (color === 'red'){
- elem[0].style.backgroundColor = color
- } else if(color === 'yellow'){
- elem[1].style.backgroundColor = color
- } else if(color === 'green'){ // у trafficLightForPedestrians тоже три div просто желтый невидим,поэтому зеленый [2]
- elem[2].style.backgroundColor = color
- }
- }
- async function trafficLightForPedestrians(){
- while(true){
- light(lightsForPedestrians,'red');
- await counter(25000, 17000,lightsForPedestrians[0],1).then(func => func(1)); //// задержка чтобы не наглели (c 16 cek)
- await Promise.race([counter(16000, 1000,lightsForPedestrians[0],1), domEventPromise(btnTurnOn, "click")]).then(func => func(1));
- light(lightsForPedestrians,'green');
- await counter(10000, 1000,lightsForPedestrians[2],1).then(func => func(1)); //func(1) или func(2) обнуление счетчика(из массива currentParent)
- }
-
- }
- async function trafficLightForCars(){
- while(true){
- light(lightsForCars,'yellow');
- await counter(3000, 1000,lightsForCars[1],0).then(func => func(0));
- light(lightsForCars,'green');
- await counter(22000, 17000,lightsForCars[2],0).then(func => func(0)); ///задержка чтобы не наглели на (c 16 cek)
- await Promise.race([counter(16000, 1000,lightsForCars[2],0),domEventPromise(btnTurnOn, "click")]).then(func => func(0));
- light(lightsForCars,'red');
- await counter(10000, 1000,lightsForCars[0],0).then(func => func(0));
- }
- }
- trafficLightForPedestrians()
- trafficLightForCars();
- //////////////////////////////////speedTest
- async function speedTest(getPromise, count, parallel=1){
-
- let createPromiseArr = function (){
- let arrPromise = []
- for (let i = 0; i<parallel; i++){
- arrPromise.push(getPromise())
- }
- return arrPromise
- }
- let startDuration = performance.now();
- for (let i = 0; i < count; i++){
- await Promise.all(createPromiseArr())
- }
- let endDuration = performance.now();
- let duration = endDuration - startDuration;
- let querySpeed = count /duration;
- let queryDuration = duration / count;
- let parallelSpeed = count * parallel / duration;
- let parallelDuration = duration / (count * parallel);
- return {
- duration,
- querySpeed,
- queryDuration,
- parallelSpeed,
- parallelDuration
- }
- }
- speedTest(() => delay(1000), 10, 10 ).then(result => console.log(result));
|