Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

settimeout problem

Question posted by: Gordan (Guest) on July 20th, 2005 10:33 AM
hi

i want to have a "close" button that user can click but that will also
"click itself after 10 sec"
so heres what i did

function auto_close(x)
{
if(x>0){ document.form.button.value="CLOSE (auto close in "+ x +" sec)";
x--;
setTimeout("auto_close(x);",1000);
} else self.close();
}

and i put onload=auto_close(10)

the problem is that the first time the recursion is called (
setTimeout("auto_close(x);",1000);) IE says x is undifined
i dont understand!!

please help, Gordan


Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Douglas Crockford's Avatar
Douglas Crockford
Guest
n/a Posts
July 20th, 2005
10:33 AM
#2

Re: settimeout problem
> i want to have a "close" button that user can click but that will also[color=blue]
> "click itself after 10 sec"[/color]
[color=blue]
> function auto_close(x)
> {
> if (x > 0){ document.form.button.value="CLOSE (auto close in "+ x +" sec)";
> x--;
> setTimeout("auto_close(x);", 1000);
> } else self.close();
> }
>
> and i put onload = auto_close(10);
>
> the problem is that the first time the recursion is called (
> setTimeout("auto_close(x);",1000);) IE says x is undifined[/color]

x is a local var of auto_close. When setTimeout finally evaluates the string, it
is not inside of auto_close, so the value of x is not available.

You can get around this by passing a function instead.

setTimeout(function () {
auto_close(x);
}, 1000);

Static binding gives that function access to x.

http://www.crockford.com/javascript/inheritance.html


Gordan's Avatar
Gordan
Guest
n/a Posts
July 20th, 2005
10:33 AM
#3

Re: settimeout problem
Thanks!

Gordan :-)


"Douglas Crockford" <nospam@laserlink.net> wrote in message
news:bkc866$bvu$1@sun-news.laserlink.net...[color=blue][color=green]
> > i want to have a "close" button that user can click but that will also
> > "click itself after 10 sec"[/color]
>[color=green]
> > function auto_close(x)
> > {
> > if (x > 0){ document.form.button.value="CLOSE (auto close in "+ x +"[/color][/color]
sec)";[color=blue][color=green]
> > x--;
> > setTimeout("auto_close(x);", 1000);
> > } else self.close();
> > }
> >
> > and i put onload = auto_close(10);
> >
> > the problem is that the first time the recursion is called (
> > setTimeout("auto_close(x);",1000);) IE says x is undifined[/color]
>
> x is a local var of auto_close. When setTimeout finally evaluates the[/color]
string, it[color=blue]
> is not inside of auto_close, so the value of x is not available.
>
> You can get around this by passing a function instead.
>
> setTimeout(function () {
> auto_close(x);
> }, 1000);
>
> Static binding gives that function access to x.
>
> http://www.crockford.com/javascript/inheritance.html
>[/color]



 
Not the answer you were looking for? Post your question . . .
183,631 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors