473,486 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

settimeout and this keyword

Hi everyone,

I am modifying the suckerfish dropdown code to use settimeout to have
a slight pause before the menus disappear to make it more user
friendly. I have hit a snag with the following statement:

li.onmouseout=function()
{
timerID=setTimeout('this.getElementsByTagName("UL" )
[0].style.display = "none"', timecount);
}

If I put take the statement

this.getElementsByTagName("UL")[0].style.display = "none";

out of the settimeout function, it works. (w/o the pause, obviously).
With the settimeout function, I get "this.getElementByTagName is not a
function" error.

I have tried putting this.getElementsByTagName("UL")[0] is a variable
and then using that in the settimeout function, but then each li will
only open the very last menu in the list. (ie all the menu items open
the last sub menu)

Here is a link to a stripped down version of the code.
http://mustang.millersville.edu/~hr/index3.php

Thanks everyone!

Apr 3 '07 #1
4 5177
On Apr 3, 2:20 pm, "Adam Risser" <aris...@gmail.comwrote:
Hi everyone,

I am modifying the suckerfish dropdown code to use settimeout to have
a slight pause before the menus disappear to make it more user
friendly. I have hit a snag with the following statement:

li.onmouseout=function()
{
timerID=setTimeout('this.getElementsByTagName("UL" )
[0].style.display = "none"', timecount);
timerID=setTimeout(function(){
this.getElementsByTagName("UL")[0].style.display = "none"
}, timecount);
>
}
Apr 3 '07 #2
On Apr 3, 3:57 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On Apr 3, 2:20 pm, "Adam Risser" <aris...@gmail.comwrote:
Hi everyone,
I am modifying the suckerfish dropdown code to use settimeout to have
a slight pause before the menus disappear to make it more user
friendly. I have hit a snag with the following statement:
li.onmouseout=function()
{
timerID=setTimeout('this.getElementsByTagName("UL" )
[0].style.display = "none"', timecount);
this solution doesn't work:
timerID=setTimeout(function(){
you can't use the "this" keyword here. you are no longer in the scope
of the "li" element, hence it "this" no longer points to the li
element.
this.getElementsByTagName("UL")[0].style.display = "none"
}, timecount);
}

try this code instead:
li.onmouseout=function() {
var that = this; //create a pointer to this reference
timerID=setTimeout(function(){
that.getElementsByTagName("UL")[0].style.display = "none"
}, 500);
}

Apr 3 '07 #3
On Apr 3, 5:16 pm, "Walton" <jrhol...@gmail.comwrote:
On Apr 3, 3:57 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On Apr 3, 2:20 pm, "Adam Risser" <aris...@gmail.comwrote:
Hi everyone,
I am modifying the suckerfish dropdown code to use settimeout to have
a slight pause before the menus disappear to make it more user
friendly. I have hit a snag with the following statement:
li.onmouseout=function()
{
timerID=setTimeout('this.getElementsByTagName("UL" )
[0].style.display = "none"', timecount);

this solution doesn't work:
timerID=setTimeout(function(){

you can't use the "this" keyword here. you are no longer in the scope
of the "li" element, hence it "this" no longer points to the li
element.
this.getElementsByTagName("UL")[0].style.display = "none"
}, timecount);
}

try this code instead:

li.onmouseout=function() {
var that = this; //create a pointer to this reference
timerID=setTimeout(function(){
that.getElementsByTagName("UL")[0].style.display = "none"
}, 500);

}
Thank you, that was part of the problem! it also took adding an extra
statement to close any active menu.

I am going to google around for some scope tutorials. Thanks again!

Apr 4 '07 #4
On Apr 4, 10:42 pm, "Adam Risser" <aris...@gmail.comwrote:
[...]
Thank you, that was part of the problem! it also took adding an extra
statement to close any active menu.
As a usability issue, consider cancelling the timeout if the cursor
goes back over the menu before it is closed.
>
I am going to google around for some scope tutorials. Thanks again!
Try the following, since you are using a closure to fix the issue:

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >
--
Rob

Apr 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14901
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
8708
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...
18
2159
by: Frances Del Rio | last post by:
this code is supposed to flip imgs at intervals of one second, but it flipps them much faster, it seems it flips them all in that first second, the more seconds the faster it flips them, instead of...
2
4627
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,...
4
1914
by: Telmo Costa | last post by:
I have a HTML page with a form with 2 buttons like this .... <input type="button" value="Add" onClick="ClickAdd();"/> <input type="button" value="Reset" onClick="ClickReset();"/> .... I...
12
5502
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. ...
4
5206
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
15
3751
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
6235
by: -Lost | last post by:
I am calling setTimeout within the context of an object, and whilst this exists, it refuses to be passed along to the function I call. For example: $elemById('id').change = function() { // the...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.