Leon Posted November 8, 2015 Share Posted November 8, 2015 (edited) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Untitled 1</title> </head> <body> <script language="javascript"> var smile = new Array(6); var rotate=0; var start; smile [0]= new Image(); smile [0].src="smile0.jpg"; smile [1]= new Image(); smile [1].src="smile1.jpg"; smile [2]= new Image(); smile [2].src="smile2.jpg"; smile [3]= new Image(); smile [3].src="smile3.jpg"; smile [4]= new Image(); smile [4].src="smile4.jpg"; smile [5]= new Image(); smile [5].src="smile5.jpg"; function turn(){ if(rotate==5) rotate=0; else ++rotate; document.smile.src=smile[rotate].src; } function turningpoint() { if (start) clearInterval(start); start=setInterval("turn()",400); } </script> <p><img src="smile0.jpg" name="smile"/> </p> <form> <input type="button" name="run" value=" Run " onclick="turningpoint();"/> <input type="button" name="Stop" value=" Stop " onclick="clearInterval(start);"/> </form> </body> </html> I am trying to do a debug on this code but i cant find any errors. I have a few questions about this code. 1) "Sometime it works on desktop but not on expression web. 2) when i rename It has nothing to do with ''var Start" to other names it doesnt work. The stop button doesnt work. For example : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Untitled 1</title> </head> <body> <script language="javascript"> var smile = new Array(6); var rotate=0; var begin; smile [0]= new Image(); smile [0].src="smile0.jpg"; smile [1]= new Image(); smile [1].src="smile1.jpg"; smile [2]= new Image(); smile [2].src="smile2.jpg"; smile [3]= new Image(); smile [3].src="smile3.jpg"; smile [4]= new Image(); smile [4].src="smile4.jpg"; smile [5]= new Image(); smile [5].src="smile5.jpg"; function turn(){ if(rotate==5) rotate=0; else ++rotate; document.smile.src=smile[rotate].src; } function turningpoint() { if (begin) clearInterval(begin); start=setInterval("turn()",400); } </script> <p><img src="../5/smile0.jpg" name="smile"/> </p> <form> <input type="button" name="run" value="Run" onclick="turningpoint();"/> <input type="button" name="Stop" value="Stop" onclick="clearInterval(begin);"/> </form> </body> </html> OR when i rename function turn() to function start() the run button doesnt function <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="en-us" http-equiv="Content-Language" /> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Untitled 1</title> </head> <body> <script language="javascript"> var smile = new Array(6); var rotate=0; var go; smile [0]= new Image(); smile [0].src="smile0.jpg"; smile [1]= new Image(); smile [1].src="smile1.jpg"; smile [2]= new Image(); smile [2].src="smile2.jpg"; smile [3]= new Image(); smile [3].src="smile3.jpg"; smile [4]= new Image(); smile [4].src="smile4.jpg"; smile [5]= new Image(); smile [5].src="smile5.jpg"; function start(){ if(rotate==5) rotate=0; else ++rotate; document.smile.src=smile[rotate].src; } function turningpoint() { if (go) clearInterval(go); start=setInterval("start()",400); } </script> <p><img src="../5/smile0.jpg" name="smile"/> </p> <form> <input type="button" name="run" value="Run" onclick="turningpoint();"/> <input type="button" name="Stop" value="Stop" onclick="clearInterval(go);"/> </form> </body> </html> Edited November 9, 2015 by Leon Link to comment Share on other sites More sharing options...
Leon Posted November 8, 2015 Author Share Posted November 8, 2015 i have been debugging all night, and i couldnt find the outcome Link to comment Share on other sites More sharing options...
dsonesuk Posted November 8, 2015 Share Posted November 8, 2015 (edited) You are changing it to a variable that has no value applied to it, so it is null.If it is null the if condition will be false it will be ignored. Edited November 8, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
Leon Posted November 8, 2015 Author Share Posted November 8, 2015 You are changing it to a variable that has no value applied to it, so it is null.If it is null the if condition will be false it will be ignored. a variable that has no value applied to it? can you show me an example ? please? a variable that has no value applied to it? can you show me an example ? please? Link to comment Share on other sites More sharing options...
dsonesuk Posted November 8, 2015 Share Posted November 8, 2015 (edited) It works OK! in better browsers, with crappy IE it fails as usual, maybe clearInterva() is overridden by setInterval() as it loops and IE being slow can't initiate clearInterval() in time, suggest you create new function and use clearInterval() within that to stop animation and remove if(start) condition. Edited November 8, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
Leon Posted November 8, 2015 Author Share Posted November 8, 2015 It works OK! in better browsers, with crappy IE it fails as usual, maybe clearInterva() is overridden by setInterval() as it loops and IE being slow can't initiate clearInterval() in time, suggest you create new function and use clearInterval() within that to stop animation and remove if(start) condition. which one it works ? the one with var start; ? if the one with var start; works then why wouldnt the one with var go; doesnt work ? They are identical. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 8, 2015 Share Posted November 8, 2015 (edited) The first one, the others won't because var go; is just declaration of empty var with no value, any reference to it will be empty nul value. Just compare it to all refereces of start, where if you look CAREFULLY it is assigned a specific method that runs a specific function (alert big clue here). Edited November 8, 2015 by dsonesuk Link to comment Share on other sites More sharing options...
Leon Posted November 8, 2015 Author Share Posted November 8, 2015 The first one, the others won't because var go; is just declaration of empty var with no value, any reference to it will be empty nul value. Just compare it to all refereces of start, where if you look CAREFULLY it is assigned a specific method that runs a specific function (alert big clue here). sorry for saying this but i did compare it ... i still dont see it... can u point out the code for me .. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 8, 2015 Share Posted November 8, 2015 Sorry i can't, its so obvious i can't believe you can't see it, LOOOK, I'm not going to do your homework for you. Link to comment Share on other sites More sharing options...
davej Posted November 8, 2015 Share Posted November 8, 2015 Terrible code. Why would you be testing a variable when you never gave it a value? Also you use the variable 'start' without declaring it. Link to comment Share on other sites More sharing options...
Leon Posted November 8, 2015 Author Share Posted November 8, 2015 Terrible code. Why would you be testing a variable when you never gave it a value? Also you use the variable 'start' without declaring it. im just following my professor example. THATS WHY IM CONFUSE.. var start doesnt have a declare but it works but var go doesnt ... Link to comment Share on other sites More sharing options...
davej Posted November 8, 2015 Share Posted November 8, 2015 Always declare variables and always assign variables a value before expecting them to have a value. What do you think is going to change the value of the 'go' variable? Link to comment Share on other sites More sharing options...
dsonesuk Posted November 8, 2015 Share Posted November 8, 2015 Any variable declared within a function without starting with var is global and accessible outside and inside the function as though it was declared outside the function starting with var. That is why variable start is always working.A variable declared in function starting with var is only accessible within that function.var go; is just declaring a varible, it has no value, method, function, absolutely nothing applied to it later on within the script, yet you are testing for a value in the if condition which will ALWAYS be false because it has a empty, null value.That it! If you don't get now i should give it up. 1 Link to comment Share on other sites More sharing options...
Leon Posted November 9, 2015 Author Share Posted November 9, 2015 (edited) Any variable declared within a function without starting with var is global and accessible outside and inside the function as though it was declared outside the function starting with var. That is why variable start is always working.A variable declared in function starting with var is only accessible within that function.var go; is just declaring a varible, it has no value, method, function, absolutely nothing applied to it later on within the script, yet you are testing for a value in the if condition which will ALWAYS be false because it has a empty, null value.That it! If you don't get now i should give it up. well, i can always rename to var start as var go for example : this is how i look at the code Edited November 9, 2015 by Leon Link to comment Share on other sites More sharing options...
dsonesuk Posted November 9, 2015 Share Posted November 9, 2015 BUT! in doing that, you have to replace every reference to start with go. Link to comment Share on other sites More sharing options...
Leon Posted November 9, 2015 Author Share Posted November 9, 2015 Any variable declared within a function without starting with var is global and accessible outside and inside the function as though it was declared outside the function starting with var. That is why variable start is always working.A variable declared in function starting with var is only accessible within that function.var go; is just declaring a varible, it has no value, method, function, absolutely nothing applied to it later on within the script, yet you are testing for a value in the if condition which will ALWAYS be false because it has a empty, null value.That it! If you don't get now i should give it up. the most importantly it was "start=setInterval("start()",400);" i have to change it to go=setInterval("turn()",400); thanks Link to comment Share on other sites More sharing options...
Leon Posted November 9, 2015 Author Share Posted November 9, 2015 BUT! in doing that, you have to replace every reference to start with go. the most importantly it was "start=setInterval("start()",400);" i have to change it to go=setInterval("turn()",400); this is the part where i ignored. im sorry for wasting your time. But thankyou for everything Link to comment Share on other sites More sharing options...
dsonesuk Posted November 9, 2015 Share Posted November 9, 2015 BINGO, EUREKA, YIPPEE KI YAY, BY JOVE I think he finally got it Holmes! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now