123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /* function green(){
- console.log('green light');
- let div = document.getElementById('green');
- div.style.backgroundColor = 'green'
-
- }
- function yellow(){
- console.log('yellow light');
- let div = document.getElementById('yellow');
- div.style.backgroundColor = 'yellow'
-
- }
- function red(){
- console.log('red light');
- let div = document.getElementById('red');
- div.style.backgroundColor = 'red';
-
- } */
- const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
- /*
- async function trafficLight(parent,greenTime,yellowTime,redTime){
-
- for(let i=0; i<3;i++){
- let div = document.createElement('div')
- div.classList = 'round';
- parent.append(div);
- }
- while (true){
- // включаем зеленый
- parent.children[0].style.backgroundColor = 'green';
- parent.children[2].style.backgroundColor = '';
- await delay(greenTime)
- // включаем желтый
- parent.children[0].style.backgroundColor = '';
- parent.children[1].style.backgroundColor = 'yellow';
- await delay(yellowTime)
- // включаем красный
- parent.children[1].style.backgroundColor = '';
- parent.children[2].style.backgroundColor = 'red';
- await delay(redTime)
- }
- }
- trafficLight(trafficlight,4000,3000,4000); */
- function domEventPromise(parent,action){
- return new Promise((resolved,reject) =>{
- function clicked(event){
- resolved(event,
- console.log(event))
- parent.removeEventListener(action,clicked);
- }
- parent.addEventListener(action, clicked)}
-
- )
- /* const result = (event) => {
- console.log(event.detail)
- resolved(event.target)
- parent.removeEventListener(action,result);
- }
- parent.addEventListener(action,result);
- }) */
- }
- domEventPromise(knopka,'click').then((e) => console.log('event click happens', e));
- //pedestrian light
- async function pedestrianLight(parent,btn,greenTime,redTime){
-
- for(let i=0; i<2;i++){
- let div = document.createElement('div')
- div.classList = 'round';
- parent.append(div);
- }
- /* let btnRes = (btn) => new Promise((resolved,reject)=>{
- console.log(`i'm disabled`);
- resolved(setTimeout(btn.disabled = true, 10000))
- }) */
- let buttonResolver = (knopka) => new Promise(() => {
- setTimeout(() => {
- // console.log('start');
- knopka.disabled = ""}, 10000);
- }, () => reject)
- while (true){
- // включаем зеленый
- parent.children[0].style.backgroundColor = 'green';
- parent.children[1].style.backgroundColor = '';
- await /* delay(greenTime) */ Promise.race([delay(greenTime),buttonResolver(btn)])
- // включаем красный
- parent.children[0].style.backgroundColor = '';
- parent.children[1].style.backgroundColor = 'red';
- await /* delay(redTime) */ Promise.race([delay(redTime),domEventPromise(knopka,'click')])
- }
- }
- pedestrianLight(trafficlight,knopka,15000,10000)
- async function speedtest(getPromise, count,parallel=1){
- let duration = performance.now();
- let initedParalel = parallel;
- let promiseArr = [];
- for(let i = 0; i < count; i++){
- promiseArr[i] = getPromise();
- parallel-=1;
- await Promise.all(promiseArr);
- }
- console.log(promiseArr);
- duration = performance.now() - duration
- console.log(initedParalel*count);
- return {
- duration:duration,
- querySpeed: count/duration, //средняя скорость одного запроса
- queryDuration:duration/count, //
- parallelSpeed: count/duration*initedParalel,
- parallelDuration: duration/(initedParalel*count)
- }
- }
-
- speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result))
- // {duration: 10000,
- // querySpeed: 0.001, //1 тысячная запроса за миллисекунду
- // queryDuration: 1000, //1000 миллисекунд на один реальный запрос в среднем
- // parallelSpeed: 0.01 // 100 запросов за 10000 миллисекунд
- // parallelDuration: 100 // 100 запросов за 10000 миллисекунд
- /* speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 1, 2) */
|