ixcese Posted September 7, 2011 Share Posted September 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246670-noob-question-what-the-difference/ Share on other sites More sharing options...
joe92 Posted September 7, 2011 Share Posted September 7, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/246670-noob-question-what-the-difference/#findComment-1266625 Share on other sites More sharing options...
nogray Posted September 8, 2011 Share Posted September 8, 2011 You also need to make sure the toDisplay2 output is inside a quote. The final code in the settimeout should be valid javascript. setTimeout("human2( '" + toDisplay2 + "' )" , 500); Quote Link to comment https://forums.phpfreaks.com/topic/246670-noob-question-what-the-difference/#findComment-1266711 Share on other sites More sharing options...
ixcese Posted September 8, 2011 Author Share Posted September 8, 2011 thank you , it's working great ! Quote Link to comment https://forums.phpfreaks.com/topic/246670-noob-question-what-the-difference/#findComment-1266757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.