473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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
Jul 20 '05 #1
2 17263
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jupiter49 | last post by:
I'm trying to move pictures around on the screen, with a slight delay between the first picture and second picture (following onmousemove). When I add the setTimeout function I must be doing it...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
29
by: Mic | last post by:
Goal: delay execution of form submit Code (Javascript + JScript ASP): <% Response.Write("<OBJECT ID='IntraLaunch' STYLE='display : none' WIDTH=0 HEIGHT=0...
4
by: Keon | last post by:
Hoi again... I trie to recall a function after 15 sec. the code I use is like this function reloads() { switch (stPage) { case 0: setTimeout("document.getElementById('myIframe').src...
3
by: Dica | last post by:
this works: document.all.style.visibility = 'hidden'; this doesn't setTimeout("document.all.style.visibility = 'hidden'",1000); looks like sResponseDivID isn't recognized. what's the problem?...
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
28
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
7
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.