Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16505 > unrolled thread

Trouble with settimeout()

Started byjoe <mt@invalid.com>
First post2012-10-09 20:35 +0300
Last post2012-10-10 10:42 +0300
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#16505 — Trouble with settimeout()

Fromjoe <mt@invalid.com>
Date2012-10-09 20:35 +0300
SubjectTrouble with settimeout()
Message-ID<9nn878d2mebc6rbrudoknmm4klm11nijkj@4ax.com>
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";}

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.



///----------- Full working example:

<html><head>
<style type="text/css">
a {text-decoration:underline;} 
.mymenu ul {
margin: 0; padding: 0;
list-style-type: none;
width: 100px;   
border-bottom: 1px solid #ccc;
}
.mymenu ul li {
position: relative;
}
.mymenu ul li a{
display: block;
overflow: auto;
padding: 7px;
color: white;
text-decoration: none;
border-bottom: 1px solid #778; border-right: 1px solid #778;
}
.mymenu ul li a:link, .mymenu ul li a:visited, .mymenu ul li a:active {
background-color: red;
} 
.mymenu ul li a:hover {
background-color: black;
}
.mymenu ul li ul {
position: absolute;
width : 150px;
top: 0;
visibility: hidden;
}
* html .mymenu ul li { float: left; height: 1%; }
* html .mymenu ul li a { height: 1%; }
</style>

<script type="text/javascript">
function menustart() { 
var ul_level=document.getElementById("mymenu1").getElementsByTagName("ul");
for (var i=0; i<ul_level.length; i++) {
    if (ul_level[i].parentNode.parentNode.id=="mymenu1")
ul_level[i].style.left=ul_level[i].parentNode.offsetWidth+"px" ;
    else
ul_level[i].style.left=ul_level[i-1].getElementsByTagName("a")[0].offsetWidth+"px"
;
   ul_level[i].parentNode.onmouseover=function()
{this.getElementsByTagName("ul")[0].style.display="block";}
   ul_level[i].parentNode.onmouseout=function()
{this.getElementsByTagName("ul")[0].style.display="none";}
   //ul_level[i].parentNode.onmouseout=function() {   // does not work
   //     timou=setTimeout(function()
{this.getElementsByTagName("ul")[0].style.display="none";},1000);
   //     }
   }
for (var i=ul_level.length-1; i>-1; i--)    { 
   ul_level[i].style.visibility="visible";
   ul_level[i].style.display="none";   }
}
if (window.addEventListener)
   window.addEventListener("load", menustart, false);
else if (window.attachEvent)
   window.attachEvent("onload", menustart);
</script>

</head> <body>
<div class="mymenu" style="position: absolute;left: 10pt; top: 20px;border: solid
1px;">
<ul id="mymenu1">
<li><a href="#">Menu item 1</a></li>
<li><a href="#">Menu item 2</a>
  <ul>
  <li><a href="#">Sub sub item 1</a></li>
  <li><a href="#">Sub sub item 2</a></li>
  </ul>
</li>  
<li><a href="#">Menu item 3</a>
  <ul>
  <li><a href="#">Sub item 1</a>
     <ul>
     <li><a href="#">Sub sub item 1</a></li>
     </ul>
  </li>   
  <li><a href="#" id="runko">Sub item 2</a></li>
  </ul>
</li>
</ul>
</div>
</body></html>

[toc] | [next] | [standalone]


#16508

FromStefan Weiss <krewecherl@gmail.com>
Date2012-10-09 20:42 +0200
Message-ID<k51r79$sj5$1@news.albasani.net>
In reply to#16505
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

[toc] | [prev] | [next] | [standalone]


#16526

Fromjoe <mt@invalid.com>
Date2012-10-10 10:42 +0300
Message-ID<vj9a785e7hh79uvoro851dfmr171dp5evd@4ax.com>
In reply to#16508
Can you elaborate further. No matter what I try teh best result I get is all sub
menus disappearing after the timeout is executed.


Stefan Weiss <krewecherl@gmail.com> wrote:

>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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web