Jump to content

noob question - what the difference?


ixcese

Recommended Posts

hey , i got 2 function , the one working and the other one isn't

 

here's the functions :

 

                        var toDisplay = "f";
		function human(){
			var toAdd = toDisplay.substring(0,1);
			setTimeout(human , 500);
			document.getElementById('box').innerHTML += toAdd;
		}
		human(); // works (display "f" every half a sec)

		function human2(toDisplay2){
			var toAdd = toDisplay2.substring(0,1);
			var t = setTimeout("human2(toDisplay2)" , 500);
			document.getElementById('box').innerHTML += toAdd;
		}
		human2("dsd"); // not working (only display "d" and stop)

 

the first function works ( keep printing "f" every 0.5 sec )

the second function only work once and than stop ( print "d" and than stop )

 

what's the problem with the second function?

 

thanks , Mor.

Link to comment
https://forums.phpfreaks.com/topic/246670-noob-question-what-the-difference/
Share on other sites

If it is running once then failing it is the timeout which is failing. I would think it is because you are trying to send a vaiable through the timeout but still have it enclosed in " hence making it part of the string. Try this:

 

setTimeout("human2(" + toDisplay2 + ")" , 500);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.