Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #16508
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Trouble with settimeout() |
| Date | 2012-10-09 20:42 +0200 |
| Organization | albasani.net |
| Message-ID | <k51r79$sj5$1@news.albasani.net> (permalink) |
| References | <9nn878d2mebc6rbrudoknmm4klm11nijkj@4ax.com> |
On 2012-10-09 19:35, joe wrote:
> Here's full working example of a vertical menu with sublevels. Whar I am trying
> to achive is a time.out from the menu. ie when mouse-cursor leaves the menu item
> it should not immediately disappear - it shout stay a 1000 msecs before
> disappearing. My efforts with setTimeout are unsuccessfull.
>
> In the code there is
>
> ul_level[i].parentNode.onmouseout=function()
> {this.getElementsByTagName("ul")[0].style.display="none";}
When you post code on Usenet, please keep your lines short enough to
prevent unintentional wrapping.
> which I trying to delay a little. Unfortunately
>
> ul_level[i].parentNode.onmouseout=function() {
> timou=setTimeout(function() {
> this.getElementsByTagName("ul")[0].style.display="none";},1000); }
>
> does not work. The menu just stays and does not go away after 100 msecs.
You might have mentioned that you also get an error message at this
point: "TypeError: Object [object Window] has no method
'getElementsByTagName'".
If you didn't get an error message, search the web for how to enable JS
debugging in your browser. There should also be something in the FAQ
about debuggers.
When the timeout fires and your inner function is executed, "this"
refers to the global object. You wanted it to refer to one of your menu
elements, but that reference is gone now. You have to store the "this"
value somewhere, so that the element reference is still available when
the function is called:
ul_level[i].parentNode.onmouseout = function() {
var that = this;
setTimeout(function() {
that.getElementsByTagName("ul")[0].style ...
}, 1000);
}
From what I've seen, you also need to add some code to clear the
timeouts when the user moves between menu items.
- stefan
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Trouble with settimeout() joe <mt@invalid.com> - 2012-10-09 20:35 +0300
Re: Trouble with settimeout() Stefan Weiss <krewecherl@gmail.com> - 2012-10-09 20:42 +0200
Re: Trouble with settimeout() joe <mt@invalid.com> - 2012-10-10 10:42 +0300
csiph-web