atar.yosef Posted September 20, 2012 Share Posted September 20, 2012 Hi there!!Just wanted to know please, if this is possible to pass A VARIABLE as parameters into a function that is called by the 'setTimeout' function, for example;setTimeout("myFunction(myParameter)", 1000);I tried to do so but with no success. Can anyone help me?Thanks in advance!!Atar. Link to comment Share on other sites More sharing options...
Don E Posted September 20, 2012 Share Posted September 20, 2012 (edited) You would pass in the parameters for the function after the delay parameter.. for example: var timeOut = setTimeout(myFunction, delay, param1, param2); For the parameter for the function, best to call the function by reference without any quotes(instead of as a string). Edited September 20, 2012 by Don E Link to comment Share on other sites More sharing options...
atar.yosef Posted September 20, 2012 Author Share Posted September 20, 2012 Thanks you about your response. I don't know what are you meaning by 'the delay parameter'. Can you explain yourself in more detail? Also, how can I call to a function by a reference rather than as a string?Thanks in advance!!Atar. Link to comment Share on other sites More sharing options...
Don E Posted September 20, 2012 Share Posted September 20, 2012 (edited) var timeOut = setTimeout(myFunction, this is the delay parameter, param1, param2); You would set that whatever delay of time you want before calling the function. To call function by reference, call it with no quotes and parenthesis. Here's a full example: <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Set Time out</title><script type="text/javascript">var timeOut; function setGreet() { timeOut = setTimeout(sayGreet, 3000, "Hello", "There!"); //waits 5 seconds before calling function sayGreet() } function sayGreet(greet1, greet2) { alert(greet1 + " " + greet2); clearTimeout(timeOut); } </script></head> <body> <button type="button" onClick="setGreet();">Say Hello!</button></body></html> Edited September 20, 2012 by Don E Link to comment Share on other sites More sharing options...
smiles Posted September 20, 2012 Share Posted September 20, 2012 (edited) Try this timeout = setTimeout(function(){ sayGreet("Hello", "There!");}, 5000); Edited September 20, 2012 by smiles 1 Link to comment Share on other sites More sharing options...
atar.yosef Posted September 20, 2012 Author Share Posted September 20, 2012 @ Don E:Thank you about your kind support!Unfortunately, your example don't meet my needs because you used at string literals as parameters while I need to use VARIABLES as parameters.Thanks in advance!!Atar. Link to comment Share on other sites More sharing options...
Ingolme Posted September 20, 2012 Share Posted September 20, 2012 The example given by smiles should work well with variables. I don't think that Don E's method works in all browsers. 1 Link to comment Share on other sites More sharing options...
Don E Posted September 20, 2012 Share Posted September 20, 2012 (edited) In place of the string literals, you can have the variables there instead, but to be on the safe side, in case it doesn't work in older browsers, go with the example smiles gave. However, the example I gave you works.. according to testing it.. in up to date browsers(except IE ) Edited September 20, 2012 by Don E Link to comment Share on other sites More sharing options...
thescientist Posted September 20, 2012 Share Posted September 20, 2012 (edited) IE's a pretty big part of the browser community to leave out, but I agree that smiles' example is the way to go. again, just replace the string literals with your desired variables. Edited September 20, 2012 by thescientist 1 Link to comment Share on other sites More sharing options...
ShadowMage Posted September 21, 2012 Share Posted September 21, 2012 IE's a pretty big part of the browser community to leave out....Unfortunately..... (Though I hear it's getting better with IE 9 & 10) Link to comment Share on other sites More sharing options...
atar.yosef Posted September 24, 2012 Author Share Posted September 24, 2012 The example given by smiles should work well with variables. I don't think that Don E's method works in all browsers.Thanks you! I tried it and it works fine. Thanks to smiles too. 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