Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > settimeout problem

Reply
Thread Tools

settimeout problem

 
 
Gordan
Guest
Posts: n/a
 
      09-18-2003
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


 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      09-18-2003
> i want to have a "close" button that user can click but that will also
> "click itself after 10 sec"


> 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


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

 
Reply With Quote
 
 
 
 
Gordan
Guest
Posts: n/a
 
      09-18-2003
Thanks!

Gordan


"Douglas Crockford" <(E-Mail Removed)> wrote in message
news:bkc866$bvu$(E-Mail Removed)...
> > i want to have a "close" button that user can click but that will also
> > "click itself after 10 sec"

>
> > 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

>
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple n00b setTimeout or onload problem vezquex@gmail.com Javascript 3 03-28-2006 03:46 AM
javascript problem with setTimeout iam Javascript 0 05-17-2005 02:32 PM
setTimeout event problem?? BKDotCom Javascript 2 12-12-2004 09:41 PM
setTimeout() problem? nobody Javascript 3 12-10-2003 02:26 PM
I have a problem with the setTimeout() function. Test Javascript 0 07-02-2003 11:14 AM



Advertisments