setTimeout

N

News

I am trying to discover what I am doing incorrectly here.

<snippet>

function not_Working(me)
{
//do something;
setTimeout("not_Working(me)",1500);//use variable
}

function Working(me)
{
//do something;
setTimeout("not_Working(3)",1500);//use constant
}

</snippet>

Why will setTimeout not work if I try to pass a variable in the function
call?
 
V

VK

News said:
I am trying to discover what I am doing incorrectly here.

<snippet>

function not_Working(me)
{
//do something;
setTimeout("not_Working(me)",1500);//use variable
}

JavaScript doesn't resolve variables in double quotes (nor in apos). So
1.5sec later not_Working function is simply being called with string
argument "me".

You want this:

setTimeout("now_Working("+me+")",1500);
 
N

News

VK said:
JavaScript doesn't resolve variables in double quotes (nor in apos). So
1.5sec later not_Working function is simply being called with string
argument "me".

You want this:

setTimeout("now_Working("+me+")",1500);

Thanks it is working
 
M

Michael Winter

JavaScript doesn't resolve variables in double quotes (nor in apos).

Insofar as the value of the second string below,

var myVariable = 'value';

'myVariable'

is just the character sequence, 'myVariable', yes.
So 1.5sec later not_Working function is simply being called with string
argument "me".

No, it isn't. Its argument is the identifier, me. The problem is that by
the time the timer has elapsed, the function will have returned and the
intended variable will no longer exist.
You want this:

setTimeout("now_Working("+me+")",1500);

The OP /might/ want that, depending upon what the value of the variable,
me, is. If it's an object reference or a string, it's almost certainly
an inappropriate suggestion.

Mike
 
L

Lasse Reichstein Nielsen

VK said:
JavaScript doesn't resolve variables in double quotes (nor in apos). So
1.5sec later not_Working function is simply being called with string
argument "me".

No.
1.5 seconds later, the string "not_Working(me)" is evaluated in the
global context. Since there is no variable called "me" in the global
context, it will give an error (undefined variable or something similar).

In non-ancient browsers, you can pass a function as the first parameter
to setTimeout. Function values are closures, including the values of their
free variables, so the following will work:

function notWorking(me) {
// ...
setTimeout(function(){not_Working(me);}, 1500);
}
You want this:

setTimeout("now_Working("+me+")",1500);

Most likely not. This only works if the value in "me" is a number,
boolean, undefined or null. If it is a string, you must quote it
and possibly escape its contents. If it is an object, it's just not
possible to make a string representation that preserves object
identity (even if you can create an expression that generates a
similiar object).


/L
 
M

Michael Winter

The problem is that by the time the timer has elapsed, the function
will have returned and the intended variable will no longer exist.

Whilst that's technically true, it's irrelevant: as Lasse, wrote, the
variable would have to be global.

Sorry, a lapse in memory.

Mike
 
N

News

Lasse Reichstein Nielsen said:
No.
1.5 seconds later, the string "not_Working(me)" is evaluated in the
global context. Since there is no variable called "me" in the global
context, it will give an error (undefined variable or something similar).

In non-ancient browsers, you can pass a function as the first parameter
to setTimeout. Function values are closures, including the values of their
free variables, so the following will work:

function notWorking(me) {
// ...
setTimeout(function(){not_Working(me);}, 1500);
}


Most likely not. This only works if the value in "me" is a number,
boolean, undefined or null. If it is a string, you must quote it
and possibly escape its contents. If it is an object, it's just not
possible to make a string representation that preserves object
identity (even if you can create an expression that generates a
similiar object).


/L
--
Lasse Reichstein Nielsen - (e-mail address removed)
DHTML Death Colors:
<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Thanks for the explaination and the code.
 
V

VK

Michael said:
No, it isn't. Its argument is the identifier, me. The problem is that by
the time the timer has elapsed, the function will have returned and the
intended variable will no longer exist.


The OP /might/ want that, depending upon what the value of the variable,
me, is. If it's an object reference or a string, it's almost certainly
an inappropriate suggestion.

Damn right. Sorry - a temporary Perl poisoning.

Oh my - I better hide myself for a week or two before Richard seen
it... :-(

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top