123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- let red = document.querySelector('.red'),
- yellow = document.querySelector('.yellow'),
- green = document.querySelector('.green'),
- btn = document.querySelector('.butn'),
- counter = document.querySelector('.counter');
- let shwCounts,timerss,startTime = true;
- const doo=ms=>new Promise(ok=>setTimeout(()=> ok(ms),ms));
-
- let low = ms => new Promise(function(res){
- btn.onclick = () => {
- buttonClick();
- clearTimeout(timerss);
- }
- timerss=setTimeout(()=>res(),ms);
- function buttonClick(){
- setTimeout(() => res(),4000);
- };
- });
- let redTimer = 6000,
- yellowTimer = 2000,
- greenTimer = 8000;
-
- async function trafficLight(){
- while (true){
- shw(parseInt(redTimer));
- yellow.classList.remove('burn-yellow');
- red.classList.add('burn-red');
- await doo(redTimer);
-
- shw(parseInt(yellowTimer));
- red.classList.remove('burn-red');
- yellow.classList.add('burn-yellow');
- await doo(yellowTimer);
-
- shw(parseInt(greenTimer));
- yellow.classList.remove('burn-yellow');
- green.classList.add('burn-green');
- await Promise.race([doo(greenTimer),low(greenTimer+1000)]);
-
- clearTimeout(shwCounts);
- counter.innerText = parseInt(yellowTimer/1000);
-
- shw(parseInt(yellowTimer));
- green.classList.remove('burn-green');
- yellow.classList.add('burn-yellow');
- await doo(yellowTimer);
- };
- };
- trafficLight();
- function shw(time) {
- if (startTime){
- counter.innerText = time/1000;
- startTime = false
- }
- time -= 1000;
- if (time == 0){
- startTime = true;
- return;
- }
- shwCounts = setTimeout(()=>{
- counter.innerText = time/1000;
- shw(time);
- },1000);
- }
- async function speedTest(promis,cnt=1,paraller=1){
- let prll = paraller, obj = {}, fdr = 0;;
- function prllStart(){
- if (paraller === 1){
- paraller = prll;
- return(promis());
- }else{
- paraller--;
- prllStart();
- return(promis());
- }
- }
- let run = performance.now();
- for (let i=0; i<cnt ; i++){
- let a = performance.now();
- await Promise.all([prllStart()]);
- fdr += performance.now() - a;
- }
- obj.summParaller = paraller + ' пралельных процессов'
- obj.lasting = performance.now() - run;
- obj.parallerLasting = fdr / cnt;
- obj.parallerSpeed = paraller / (obj.parallerLasting);
- obj.durationLasting = parseInt(obj.parallerLasting) + 'ms'
- obj.requestSpeed='нет отличия'
- return obj;
- }
- speedTest(()=>doo(1000),10,100).then(result=>console.log(result));
- speedTest(()=>fetch('http://swapi.dev/api/people/1').then(res=>res.json()),10,5).then(result=>console.log(result));
|