ckrudelux Posted April 12, 2010 Share Posted April 12, 2010 fancyfunction(){alert("Hello World!");}This works:setTimeout("alert('Hello World!')", 3000);This don't:setTimeout("fancyfunction()", 3000);and if I do this it skips the delay:setTimeout(fancyfunction(), 3000);Why? Link to comment Share on other sites More sharing options...
jeffman Posted April 13, 2010 Share Posted April 13, 2010 setTimeout(fancyfunction(), 3000);This won't work because it runs fancyfunction immediately. The statement tells the timer to execute the return value of fancyfunction in 3 seconds. That's not what you mean, especially if the return value is an integer or something else that cannot be executed.Try this: var x = setTimeout(fancyfunction, 3000); Link to comment Share on other sites More sharing options...
ckrudelux Posted April 13, 2010 Author Share Posted April 13, 2010 setTimeout(fancyfunction(), 3000);This won't work because it runs fancyfunction immediately. The statement tells the timer to execute the return value of fancyfunction in 3 seconds. That's not what you mean, especially if the return value is an integer or something else that cannot be executed.Try this: var x = setTimeout(fancyfunction, 3000); Just remember that firebug is a good thing to check and I got calling undefined function in this code. Can't I call the function it self?function adminmenuopenclose(sub){ var statusid = ""; if(document.getElementById('1337bottombar').style.display == "block"){ if(document.getElementById('1337tools').style.display == "none" && sub == "tools"){ statusid = "false"; }else if(document.getElementById('1337system').style.display == "none" && sub == "system"){ statusid = "false"; }else{ statusid = "true"; } }else{ statusid = "false"; } if(document.getElementById("1337tools").style.display == "block" || document.getElementById("1337system").style.display == "block"){ if(document.getElementById("1337tools").style.display == "block" && sub == "system"){ document.getElementById("1337tools").style.display = "none"; document.getElementById("1337system").style.display = "block"; }else if(document.getElementById("1337system").style.display == "block" && sub == "tools"){ document.getElementById("1337system").style.display = "none"; document.getElementById("1337tools").style.display = "block"; } }else{ if(sub == "tools"){ document.getElementById("1337tools").style.display = "block"; }else { document.getElementById("1337system").style.display = "block"; } } function godown(){ var topplace = document.getElementById("1337bottombar").style.top; topplace = topplace.substring(0, (topplace.length-2)); if(topplace == 23){ }else { topplace++; document.getElementById("1337bottombar").style.top = topplace+"px"; var thisobject2 = setTimeout("godown()",1000); } } function goup(){ var topplace = document.getElementById("1337bottombar").style.top; topplace = topplace.substring(0, (topplace.length-2)); if(topplace == -23){ document.getElementById('1337bottombar').style.display = "none"; document.getElementById('1337tools').style.display = "none"; document.getElementById('1337system').style.display = "none"; }else{ topplace--; document.getElementById("1337bottombar").style.top = topplace+"px"; var thisobject1 = setTimeout("goup()",1000); } } if(statusid == "true"){ goup(); }else { document.getElementById("1337bottombar").style.display = "block"; godown(); }} Link to comment Share on other sites More sharing options...
jeffman Posted April 13, 2010 Share Posted April 13, 2010 A function can call itself or cause itself to be called. But remember that goup and godown exist only within the scope of adminmenuopenclose(); they are not available external to that function.When you write setTimeout("godown()",1000);. . . you are telling the timer to evalute a string and then execute the code that it finds there. Assuming the timer runs after adminmenuopenclose has terminated, your scope has changed, so goup and godown are no longer available. That's why the interpreter tells you they don't exist.When you write it this way: setTimeout(godown,1000);. . . you are telling the timer to execute a function that was passed to it by reference. Since the function object exists when the reference is passed, it continues to exist until it is needed, even after adminmenuopenclose has terminated. Within the scope of the timer, godown is still available. You have created what's called a closure.Even if this were not true, it is more efficient to pass functions by reference than to pass strings that must be evaluated before executing; so I recommend passing functions by reference whenever possible, and passing code in string form as seldom as possible (never, if you can help it). And that includes event handlers that people put in their HTML tags. Link to comment Share on other sites More sharing options...
ckrudelux Posted April 13, 2010 Author Share Posted April 13, 2010 A function can call itself or cause itself to be called. But remember that goup and godown exist only within the scope of adminmenuopenclose(); they are not available external to that function.When you write setTimeout("godown()",1000);. . . you are telling the timer to evalute a string and then execute the code that it finds there. Assuming the timer runs after adminmenuopenclose has terminated, your scope has changed, so goup and godown are no longer available. That's why the interpreter tells you they don't exist.When you write it this way: setTimeout(godown,1000);. . . you are telling the timer to execute a function that was passed to it by reference. Since the function object exists when the reference is passed, it continues to exist until it is needed, even after adminmenuopenclose has terminated. Within the scope of the timer, godown is still available. You have created what's called a closure.Even if this were not true, it is more efficient to pass functions by reference than to pass strings that must be evaluated before executing; so I recommend passing functions by reference whenever possible, and passing code in string form as seldom as possible (never, if you can help it). And that includes event handlers that people put in their HTML tags.Then I got it! Thanks alot Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.