settimeout and this keyword

A

Adam Risser

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

scripts.contact

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);
 
W

Walton

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);
}
 
A

Adam Risser

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top