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; idoo(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));