javascript - Keep a timer (coded in JS) that will automatically change color of image after 3 seconds -
i started learning js , saw below given example(in link) , thought of coding automatic bulb switch on /off modifying existing example code.
thought process : after user hits image of bulb 1st time , bulb automatically switch on / off. interval between switch on /off : 3 seconds. 1 hit whole process continues 2 mins or less.
code example : http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_lightbulb
existing code :
<!doctype html> <html> <body> <h1>javascript can change images</h1> <img id="myimage" onclick="changeimage()" src="pic_bulboff.gif" width="100" height="180"> <p>click light bulb turn on/off light.</p> <script> function changeimage() { var image = document.getelementbyid('myimage'); if (image.src.match("bulbon")) { image.src = "pic_bulboff.gif"; } else { image.src = "pic_bulbon.gif"; } } </script> </body> </html>
code modification did :
<script> function changeimage() { var t ; var image = document.getelementbyid('myimage'); while t < 90 { t++ ; settimeout() : var delay= 3000 ; if (image.src.match("bulbon")) image.src = "pic_bulboff.gif"; else image.src = "pic_bulbon.gif"; } } </script>
and above modified code nothing. working code change helpful me understand more js. thank you
var image = document.getelementbyid('myimage'); setinterval(function(){ if (image.src.match("bulbon")) { image.src = "pic_bulboff.gif"; }else { image.src = "pic_bulbon.gif"; }; },100);
this code make bubl change ever 0.1s
Comments
Post a Comment