javascript change the animation sequence without input -


what im looking add code change animation , make sequence of light go on automatically without user input..

here code far, need code change button clicking function automatic sequence of lights.

<h1>changing html images</h1> <p> click button change traffic lights </p> <img id="myimage" onclick="changeimage()" src="red.jpg" width="100" height="180">   <button type ="button" onclick="changeimage()">change traffic light</button>  <script> var counter = 0 var image = ["red.jpg","redandorange.jpg","orange.jpg","green.jpg"]  function changeimage () {      window.alert(counter)     counter++      if(counter > 3 ){         counter = 0     }      document.getelementbyid('myimage').src=image[counter] }  </script> <body> </body> </html> 

you can use setinterval(method, intervaltime) or settimeout(method, waittime).

the easiest 1 in specific case use setinterval. example:

setinterval(changeimage, 500); 

in more complicated cases, you'd want use settimeout , restart yourself. ensure there's minimum specified time between completion of method , start of following execution. example:

var next = function() {   changeimage();   settimeout(next, 500); }; next(); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -