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 trafficLight = async () =>{ while (true){ toggleLight(false,'.red'); toggleLight(true,'.green'); await delay(2000); toggleLight(false,'.green'); toggleLight(true,'.yellow'); await delay(4000); toggleLight(false,'.yellow'); toggleLight(true,'.red'); await delay(6000); } } trafficLight();