By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,473 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,473 IT Pros & Developers. It's quick & easy.

settimeout problem

P: 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
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> 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

Jul 20 '05 #2

P: n/a
Thanks!

Gordan :-)
"Douglas Crockford" <no****@laserlink.net> wrote in message
news:bk**********@sun-news.laserlink.net...
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

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.