trafficLight.js 571 B

12345678910111213141516171819202122
  1. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
  2. const toggleLight = (switcher, className) => {
  3. const element = document.querySelector(className).classList;
  4. switcher ? element.add('on') : element.remove('on');
  5. }
  6. const trafficLight = async () =>{
  7. while (true){
  8. toggleLight(false,'.red');
  9. toggleLight(true,'.green');
  10. await delay(2000);
  11. toggleLight(false,'.green');
  12. toggleLight(true,'.yellow');
  13. await delay(4000);
  14. toggleLight(false,'.yellow');
  15. toggleLight(true,'.red');
  16. await delay(6000);
  17. }
  18. }
  19. trafficLight();