Connecting Tech Pros Worldwide Help | Site Map

settimeout problem

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

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, 11:33 AM
Gordan
Guest
 
Posts: n/a

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]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
settimeout problem... sencomenco answers 1 May 8th, 2008 08:24 PM
SetTimeout Problem again - Please Help Harshpandya answers 1 April 12th, 2008 12:03 AM
setTimeout() problem? nobody answers 3 July 20th, 2005 01:41 PM
"complex" rollover animation question Brian Angliss answers 5 July 20th, 2005 11:35 AM