admin管理员组文章数量:1385024
My first post here. I want to make a horizontal menu with submenu's sliding down on mouseover. I know I could use jQuery but this is to practice my javascript skills.
I use the following code:
var up = new Array()
var down = new Array()
var submenustart
function titleover(headmenu, inter)
{
submenu = headmenu.lastChild
up[inter] = window.clearInterval(up[inter])
down[inter] = window.setInterval("slidedown(submenu)",1)
}
function slidedown(submenu)
{
if(submenu.offsetTop < submenustart)
{
submenu.style.top = submenu.offsetTop + 1 + "px"
}
}
function titleout(headmenu, inter)
{
submenu = headmenu.lastChild
down[inter] = window.clearInterval(down[inter])
up[inter] = window.setInterval("slideup(submenu)", 1)
}
function slideup(submenu)
{
if(submenu.offsetTop > submenustart - submenu.clientHeight + 1)
{
submenu.style.top = submenu.offsetTop - 1 + "px"
}
}
The variable submenustart gets appointed a value in another function which is not relevant for my question.
HTML looks like this:
<table class="hoofding" id="hoofding">
<tr>
<td onmouseover="titleover(this, 0)" onmouseout="titleout(this, 0)"><a href="#" class="hoofdinglink" id="hoofd1">AAAA</a>
<table class="menu">
<tr><td><a href="...">1111</a></td></tr>
<tr><td><a href="...">2222</a></td></tr>
<tr><td><a href="...">3333</a></td></tr>
</table></td>
<td onmouseover="titleover(this, 1)" onmouseout="titleout(this, 1)"><a href="#" class="hoofdinglink">BBBB</a>
<table class="menu">
<tr><td><a href="...">1111</a></td></tr>
<tr><td><a href="...">2222</a></td></tr>
<tr><td><a href="...">3333</a></td></tr>
<tr><td><a href="...">4444</a></td></tr>
<tr><td><a href="...">5555</a></td></tr>
</table></td>
...
</tr>
</table>
What happens is the following:
If I go over and out (for ex) menu A it works fine.
If i go now over menu B the interval applied to A is now applied to B. There are now 2 interval functions applied to B. The one originally for A and a new one triggered by the mouseover on B.
If I would go to A all the intervals are now applied to A.
I have been searching for hours but and I am pletely stuck.
Thanks in advance.
My first post here. I want to make a horizontal menu with submenu's sliding down on mouseover. I know I could use jQuery but this is to practice my javascript skills.
I use the following code:
var up = new Array()
var down = new Array()
var submenustart
function titleover(headmenu, inter)
{
submenu = headmenu.lastChild
up[inter] = window.clearInterval(up[inter])
down[inter] = window.setInterval("slidedown(submenu)",1)
}
function slidedown(submenu)
{
if(submenu.offsetTop < submenustart)
{
submenu.style.top = submenu.offsetTop + 1 + "px"
}
}
function titleout(headmenu, inter)
{
submenu = headmenu.lastChild
down[inter] = window.clearInterval(down[inter])
up[inter] = window.setInterval("slideup(submenu)", 1)
}
function slideup(submenu)
{
if(submenu.offsetTop > submenustart - submenu.clientHeight + 1)
{
submenu.style.top = submenu.offsetTop - 1 + "px"
}
}
The variable submenustart gets appointed a value in another function which is not relevant for my question.
HTML looks like this:
<table class="hoofding" id="hoofding">
<tr>
<td onmouseover="titleover(this, 0)" onmouseout="titleout(this, 0)"><a href="#" class="hoofdinglink" id="hoofd1">AAAA</a>
<table class="menu">
<tr><td><a href="...">1111</a></td></tr>
<tr><td><a href="...">2222</a></td></tr>
<tr><td><a href="...">3333</a></td></tr>
</table></td>
<td onmouseover="titleover(this, 1)" onmouseout="titleout(this, 1)"><a href="#" class="hoofdinglink">BBBB</a>
<table class="menu">
<tr><td><a href="...">1111</a></td></tr>
<tr><td><a href="...">2222</a></td></tr>
<tr><td><a href="...">3333</a></td></tr>
<tr><td><a href="...">4444</a></td></tr>
<tr><td><a href="...">5555</a></td></tr>
</table></td>
...
</tr>
</table>
What happens is the following:
If I go over and out (for ex) menu A it works fine.
If i go now over menu B the interval applied to A is now applied to B. There are now 2 interval functions applied to B. The one originally for A and a new one triggered by the mouseover on B.
If I would go to A all the intervals are now applied to A.
I have been searching for hours but and I am pletely stuck.
Thanks in advance.
Share Improve this question edited Jun 16, 2010 at 1:27 alex 491k204 gold badges889 silver badges991 bronze badges asked May 14, 2010 at 3:05 Roel V.Roel V. 551 silver badge4 bronze badges 2-
don't forget to call
clearInterval
when you're done animating. – Robert Paulson Commented Jun 16, 2010 at 3:15 - Thanks for the remark, but I was aware of this. I did add this clearInterval later. – Roel V. Commented Jun 19, 2010 at 18:47
2 Answers
Reset to default 7My first suggestion is to
- Practice using semicolons properly and not relying on semicolon insertion
- Don't pass functions as strings (which requires an implicit
eval
) - use JavaScript to attach events in a non-obtrusive manner
.
var up = [],
down = [],
submenustart;
function titleover(headmenu, inter) {
up[inter] = window.clearInterval(up[inter]);
down[inter] = window.setInterval(function() { slidedown(headmenu.lastChild); }, 1);
}
function slidedown(submenu) {
if ( submenu.offsetTop < submenustart ) {
submenu.style.top = submenu.offsetTop + 1 + "px";
}
}
function titleout(headmenu, inter) {
down[inter] = window.clearInterval(down[inter]);
up[inter] = window.setInterval(function() { slideup(headmenu.lastChild); }, 1);
}
function slideup(submenu) {
if ( submenu.offsetTop > submenustart - submenu.clientHeight + 1 ) {
submenu.style.top = submenu.offsetTop - 1 + "px";
}
}
My second suggestion is to not use tables for menus, as they aren't tabular data. Instead, use an unsorted list.
<ul class="hoofding" id="hoofding">
<li onmouseover="titleover(this, 0)" onmouseout="titleout(this, 0)">
<a href="#" class="hoofdinglink" id="hoofd1">AAAA</a>
<ul class="menu">
<li><a href="...">1111</a></li>
<li><a href="...">2222</a></li>
<li><a href="...">3333</a></li>
</ul>
</li>
<li onmouseover="titleover(this, 1)" onmouseout="titleout(this, 1)">
<a href="#" class="hoofdinglink">BBBB</a>
<ul class="menu">
<li><a href="...">1111</a></li>
<li><a href="...">2222</a></li>
<li><a href="...">3333</a></li>
</ul>
</li>
</ul>
AFAIK, (and according to the Gecko DOM reference) window.clearInterval() does not return anything, which maybe the reason which messes things up.
本文标签: javascriptProblems with multiple setIntervals running simultaneouslyStack Overflow
版权声明:本文标题:javascript - Problems with multiple setIntervals running simultaneously - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744538612a2611469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论