123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- // Светофор + domEventPromise
- // function delay(ms) {
- // return new Promise(ok => setTimeout(() => ok(ms), ms)).then(response => console.log(response));
- // }
- //
- // let div = document.createElement('div');
- // let div1 = document.createElement('div');
- // let div2 = document.createElement('div');
- //
- // div.className = 'trafficLight';
- // div1.className = 'trafficLight';
- // div2.className = 'trafficLight';
- //
- // placeForTrafficLight.appendChild(div);
- // placeForTrafficLight.appendChild(div1);
- // placeForTrafficLight.appendChild(div2);
- //
- // let promise = async function trafficLight() {
- //
- // while (true) {
- // div.style.background = 'green';
- // div1.style.background = 'white';
- // div2.style.background = 'white';
- // await delay(4000);
- // div.style.background = 'white';
- // div1.style.background = 'yellow';
- // div2.style.background = 'white';
- // await delay(2000);
- // div.style.background = 'white';
- // div1.style.background = 'white';
- // div2.style.background = 'red';
- // await delay(30000);
- // }
- // };
- //
- // promise().then(response => console.log(response));
- //
- // function domEventPromise(button, eventName) {
- // return new Promise((resolve) => {
- // button.addEventListener(eventName, (event) => {
- // resolve(event);
- // button.disabled = true;
- // });
- //
- // button.removeEventListener(eventName, (event) => {
- // resolve(event.delay(4000));
- // });
- // });
- // }
- //
- // let button = document.createElement('button');
- // button.className = 'buttonTraficLight';
- // button.textContent = 'Push';
- // placeForButton.appendChild(button);
- //
- // const pushPromise = domEventPromise(button, 'click');
- // pushPromise.then(e => console.log('event click happens', e));
- // PedestrianTrafficLight
- function delay(ms) {
- return new Promise(ok => setTimeout(() => ok(ms), ms)).then(response => console.log(response));
- }
- let div = document.createElement('div');
- let div1 = document.createElement('div');
- let div2 = document.createElement('div');
- div.className = 'trafficLight';
- div1.className = 'trafficLight';
- div2.className = 'trafficLight';
- placeForTrafficLight.appendChild(div);
- placeForTrafficLight.appendChild(div1);
- placeForTrafficLight.appendChild(div2);
- let button = document.createElement('button');
- button.className = 'buttonTraficLight';
- button.textContent = 'Push';
- placeForButton.appendChild(button);
- async function pedestrianTrafficLight() {
- while (true) {
- switch ('red') {
- case 'red':
- div.style.background = 'red';
- div1.style.background = 'white';
- div2.style.background = 'white';
- button.disabled = true;
- await delay(4000);
- button.disabled = false;
- await Promise.race([delay(10000), domEventPromise(button, 'click').then(e => console.log('event click happens', e))]);
- }
- switch ('yellow') {
- case 'yellow':
- div.style.background = 'white';
- div1.style.background = 'yellow';
- div2.style.background = 'white';
- await Promise.race([delay(1000), domEventPromise(button, 'click').then(e => console.log('event click happens', e))]);
- }
- switch ('green') {
- case 'green':
- button.disabled = true;
- div.style.background = 'white';
- div1.style.background = 'white';
- div2.style.background = 'green';
- await delay(3000);
- }
- }
- }
- pedestrianTrafficLight();
- function domEventPromise(button, eventName) {
- return new Promise((resolve, reject) => {
- button.addEventListener(eventName, (event) => {
- resolve(event);
- });
- button.removeEventListener(eventName, (event) => {
- resolve(event);
- });
- });
- }
- function delay(ms) {
- return new Promise(ok => setTimeout(() => ok(ms), ms)).then(response => console.log(response));
- }
- async function speedtest(getPromise, count, parallel = 1) {
- let counter = 0;
- let paralCount = 0;
- let arrPromises = [];
- const start = new Date().getTime();
- while (counter !== count) {
- counter++;
- while (paralCount !== parallel) {
- paralCount++;
- arrPromises.push(await getPromise());
- }
- Promise.all([arrPromises]).then(result => console.log(result));
- }
- const end = new Date().getTime();
- let duration = end - start;
- let parallelDuration = duration / (count * parallel);
- let parallelSpeed = parallel / parallelDuration;
- let queryDuration = duration / count;
- let querySpeed = count / queryDuration;
- return {
- duration,
- querySpeed,
- queryDuration,
- parallelSpeed,
- parallelDuration
- };
- }
- speedtest(() => delay(1000), 10, 10).then(result => console.log(result));
- // // speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5);
|