Connecting Tech Pros Worldwide Help | Site Map

settimeout problem

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 10:33 AM
Gordan
Guest
 
Posts: n/a
Default settimeout problem

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



  #2  
Old July 20th, 2005, 10:33 AM
Douglas Crockford
Guest
 
Posts: n/a
Default 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

  #3  
Old July 20th, 2005, 10:33 AM
Gordan
Guest
 
Posts: n/a
Default 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]


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.