1234567891011121314151617181920212223242526272829303132333435363738 |
- const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
- const toggleLight = (switcher, className) => {
- const element = document.querySelector(className).classList;
- switcher ? element.add('on') : element.remove('on');
- }
- const addDomElement = (element) => {
- for(let i = 1; i <= 3; i++) {
- const domElement = document.createElement(element);
- domElement.classList.add('traffic-light__element');
- if(i === 1) {
- domElement.classList.add('green');
- } else if (i === 2) {
- domElement.classList.add('yellow');
- } else {
- domElement.classList.add('red');
- }
- trafficLightId.appendChild(domElement);
- }
- }
- const trafficLight = async (domElement, delayGreen, delayYellow, delayRed) =>{
- addDomElement(domElement);
- while (true){
- toggleLight(false,'.red');
- toggleLight(true,'.green');
- await delay(delayGreen);
- toggleLight(false,'.green');
- toggleLight(true,'.yellow');
- await delay(delayYellow);
- toggleLight(false,'.yellow');
- toggleLight(true,'.red');
- await delay(delayRed);
- }
- }
- trafficLight('div',2000, 4000, 6000);
|