calling function with parameters in setTimeout

0 views
Skip to first unread message

Yvon Thoraval

unread,
Jun 20, 2001, 3:41:19 PM6/20/01
to
I'm using setTimeout :

setTimeout('agitBout( ce_bout, dX, dY, wTimeOut); ', milliSeconds);

when calling setTimeout i got an error message saying ce_bout is
undefined

however doing a alert(ce_bout) just before setTimeout i got right :

[object object]

and, changing setTimeout for :

setTimeout('agitBout( '+ce_bout+', '+dX+', '+dY+', '+wTimeOut+'); ',
milliSeconds);

is working on NS_4.7.7 but NOT with IE 5/mac js error message is saying
that there is a lack of "]" right there :
______________________V
setTimeout('agitBout( '+ce_bout+', '+dX

that's to say just before the "'"


the calling of setTimeout :

if (withTimeOut==true)
{
for (i =0; i < ce_bout.lyrNameAry.length; i++)
{
lyrObj[i].top = top[i] + deltaY;
lyrObj[i].left = left[i] + deltaX;
}
timeDelay(milliSeconds, ce_bout, deltaX, deltaY);
}


her ce_bout works well, this is part of a function :

function agitBout(ce_bout, deltaX, deltaY, withTimeOut)

which is calling setTimeout thru timeDelay ... :

is it possible to call back the function having called setTimeout ?

Gosha Bine

unread,
Jun 20, 2001, 5:02:58 PM6/20/01
to
You cannot pass variables to function being executed on setTimeout, since
the variables are converted to strings before call.

setTimeout("anyFunc("+anyObject+")") calls anyFunc with string argument
"[object Object]"

You can pass only the variables' names:

setTimeout("anyFunc(anyObject)")

This works ok when anyObject is defined globally. setTimeout arguments are
evaluated in global context.

Example:

function anyFunc(e){alert(e)}

function bad()
{
var anyObject1=new String(123);
setTimeout("anyFunc(anyObject1)",1); /* error */
}

function good()
{
window.anyObject2=new String(123);
setTimeout("anyFunc(anyObject2)",1); /* ok */
}

good()
bad()

--
Gosha Bine
http://www.stereofrog.com


Yvon Thoraval <yvon.b...@pobox.com> wrote in message
news:1evbl7w.1mflxgqzb6az4N%yvon.b...@pobox.com...

Yvon Thoraval

unread,
Jun 21, 2001, 3:06:52 AM6/21/01
to
Gosha Bine <strer...@hotmail.com> wrote:

> You cannot pass variables to function being executed on setTimeout, since
> the variables are converted to strings before call.

thanks a lot, it is what i've expected, now it is clear to me !

Yvon Thoraval

unread,
Jun 21, 2001, 4:12:26 AM6/21/01
to
Gosha Bine <strer...@hotmail.com> wrote:

> You cannot pass variables to function being executed on setTimeout, since
> the variables are converted to strings before call.

this works fine now, thanks again !

Reply all
Reply to author
Forward
0 new messages