Re: setTimeout
- From: Lasse Reichstein Nielsen <lrn@xxxxxxxxxx>
- Date: Tue, 18 Apr 2006 20:03:15 +0200
"VK" <schools_ring@xxxxxxxxx> writes:
News wrote:
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".
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
--
Lasse Reichstein Nielsen - lrn@xxxxxxxxxx
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
.
- Follow-Ups:
- Re: setTimeout
- From: News
- Re: setTimeout
- References:
- setTimeout
- From: News
- Re: setTimeout
- From: VK
- setTimeout
- Prev by Date: Re: Different regular expression splitting in Firefox/IE
- Next by Date: Re: ParamArrays in JavaScript functions possible?
- Previous by thread: Re: setTimeout
- Next by thread: Re: setTimeout
- Index(es):
Relevant Pages
|