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!

Referring to custom objects in functions driven by setTimeout

Status
Not open for further replies.

philcha

Programmer
May 10, 2000
109
GB
I've had lots of fun(!!??) with setTimeout, especially in conjunction with objects.<br><br>setTimeout has 2 arguments:<br>*&nbsp;&nbsp;a string which is interpreted (effectively by an eval function) on expiry of the timeout.<br>*&nbsp;&nbsp;the timeout interval.<br><br>Because the first argument is treated as a string, it's very difficult to see what's being evaluated.&nbsp;&nbsp;Generally when I use setTimeout I:<br>*&nbsp;&nbsp;build the string in a variable so if I have problems I can use an alert to display it and check it.<br>*&nbsp;&nbsp;use the string as the 1st agrumentof setTimeout, e.g. setTimeout(myString, interval);<br><br>Also because the first argument is treated as a string, on expiry of the timeout the browser does not remember any object references (&quot;this&quot; or &quot;myObj&quot;) which were valid at the time of the setTimeout call.<br><br>So in the simplest case you have to hardcode the actual name of the variable which contains the object reference in the function driven by the setTimeout call, e.g.<br>var myObj = new myObjType;&nbsp;&nbsp;// object constructor<br>setTimeout (myFunc, interval);<br>function myFunc(){<br>&nbsp;&nbsp;&nbsp;myObj.width = x;<br>}<br><br>If you want to use myFunc on a range of objects or to make the entire sequence of code which calls myFunc re-usable, things get trickier and you have to:<br>// Make the OBJECT on which myFunc operates a custom <br>// PROPERTY of a standard JS object, e.g.<br>var myObj = new myObjType;<br>document.ObjId = myObj;<br>// Create a function to call myFunc.&nbsp;&nbsp;All application code <br>// calls callMyFunc, never myFunc<br>function callMyFunc(newWidth, interval){&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;var myString = &quot;myFunc(&quot; + newWidth + &quot;, &quot; + interval + <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;)&quot;;<br>&nbsp;&nbsp;&nbsp;// This where it's handy to be able to display myString <br>&nbsp;&nbsp;&nbsp;// if you're finding it difficult to get the punctuation <br>&nbsp;&nbsp;&nbsp;// right&nbsp;&nbsp;-&nbsp;&nbsp;especially if there are several arguments <br>&nbsp;&nbsp;&nbsp;// and&nbsp;&nbsp;/ or some of the arguments are string values and <br>&nbsp;&nbsp;&nbsp;// have to be in single quotes.<br>&nbsp;&nbsp;&nbsp;&nbsp;setTimeout (myString, interval);<br>}<br>// Refer to the target object via the custom property of <br>// the standard object, e.g.<br>function myFunc(newWidth, interval){<br>&nbsp;&nbsp;&nbsp;// Here you have to use ObjId to find the required object<br>&nbsp;&nbsp;&nbsp;// see notes below.<br>&nbsp;&nbsp;&nbsp;document.ObjId.width = newWidth;<br>}<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top