Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timeout loses variable 2

Status
Not open for further replies.

eggman

Technical User
Apr 25, 2000
3
US
when I have the following:<br><br>functionX(xyz)<br>&nbsp;&nbsp;&nbsp;codecodecode[xyz];<br>&nbsp;&nbsp;&nbsp;setTimeout(&quot;functionX()&quot;,2000);<br><br>Netscape always says document[xyz] has no properties even though it found it the first time through. Why doesn't it remember it the second time round?
 
When the timeout run the function, call functionX() without parameters, if you want this params the code is:<br>setTimeout(&quot;funcionX(&quot; + x + &quot;,&quot; + y ... &quot;)&quot;,2000)<br><br>It is that you want
 
thanks. can I ask what is probably a basic programmer question? when i've looked up the plus signs in books, it says it's for concatenation but this doesn't seem like that. also, why have I seen it sometimes surrounded by single and double quotes?
 
setTimeout's function / expression argument is treated as a string which is evaluated when the timeout expires.&nbsp;&nbsp;This has 2 consequences:<br>*&nbsp;&nbsp;you can't directly pass an object as an argument to a function which is called by setTimeout.<br>*&nbsp;&nbsp;getting the punctuation right is a pain, especially if part(s) of the argument(s) to the called function are quoted string(s).&nbsp;&nbsp;This is why you can wind up with mixtures of single quotes and double quotes (usually double quotes for the strings which define the function / expression argument and single quotes for the strings you actually want to pass to the called function).<br><br>I often do the following to check that I've got the punctuation right:<br>*&nbsp;&nbsp;build the function / expression argument in a local variable.<br>*&nbsp;&nbsp;use alert to display the local variable.&nbsp;&nbsp;This allows me to see whether the call I'm generating is the call I want.<br>*&nbsp;&nbsp;pass setTimeout the variable as its function / expression argument.<br>*&nbsp;&nbsp;comment out the alert when it's working.<br><br>For example:<br>&nbsp;&nbsp;&nbsp;var X = &quot;function_name(...........)&quot;;<br>&nbsp;&nbsp;&nbsp;alert(X);<br>&nbsp;&nbsp;&nbsp;setTimeout(X, interval);<br>
 
thanks, philcha. that will be very helpful
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top