JavaScript Automation -
i using code change color of traffic lights on button click, how can change run sequence automatically once button clicked , repeat indefinitely?
var lightstates = {red:0,amber:1,green:2}; var currentstate = lightstates.red; document.getelementbyid('changebtn').onclick=function(){ changestate(); }; function changestate() { clear(); switch(currentstate) { case lightstates.red: { document.getelementbyid("red").classname ="light red"; currentstate = lightstates.amber; } break; case lightstates.amber: { document.getelementbyid("amber").classname ="light amber"; currentstate = lightstates.green; } break; case lightstates.green: { document.getelementbyid("green").classname ="light green"; currentstate = lightstates.red; } break; } } function clear(){ document.getelementbyid("red").classname ="light off"; document.getelementbyid("amber").classname ="light off"; document.getelementbyid("green").classname ="light off";
Comments
Post a Comment