/    Sign up×
Community /Pin to ProfileBookmark

setTimeout Cascading Menu not firing

Hello, I’m an old coder who thought he’d come back and try his hand at DHTML. I was never really too into JavaScript or OOP at all, but I’ve always wanted to pick up some fundamental skills for it. I thought I’d do this by trying to write another cliche drop down menu. Not for the masses, just as a learning experience for myself. And while I’ve read massive tutorials and seen so many examples, they are all so bloated, and I’d much rather tangle with a smaller beast to begin with, and to get the hang of it again. Coding is a lot like riding a bike.

I always get stuck trying to figure out a way to have the menu close after a mouse out, or what have you. Mousing out of certain areas needs to fire the closing function, but what if the mouse in that short timeout before close, rolls over a part of the menu again? I’ve seen many examples of this using the DOMs bubbling phase, but I don’t really like that way.

I figured it would theoretically be possible to just assign a variable on mouseovers of those certain areas, so that after the setTimeout is fired, when the function is finally ran, the variable will be checked first. If the variable is true (meaning that in that timeout time, the mouse is again over a menu area) then the function will not fire. Otherwise, it will, and the menu closes.

However, I’m experiencing a few issues. One is that the setTimeout doesn’t seem to be firing. I’m sure its something small and stupid I’ve done, yet overlooked. But I’ve looked many times. The function fires, but immediately, with no timeout.

Another issue I’ve had is that when the function does fire, it fires and fires and fires again. I noticed this, because instead of having the menu close, I put an alert in the function for the closing menu, for troubleshooting reasons. The alert pops up many times.

I don’t really know where else to turn, as I’m completely confused and new to this (in some ways). As I said, I have some coding experience, but it has been a while. I’ve looked through resources and decided my only option is to get a bit of human interaction. Any help or information you can provide would be great. In fact, if you know of any nice resources out there that show the concepts of this in a simple way, I’d love that too. Maybe there is one I’ve missed.

Again, I suppose this isn’t too urgent in a way. I’d just like to learn it myself, and don’t know where else to turn. Sorry this post turned out to be so long, but thank you for any help.

Here is the code. The getObj, and findPos functions are from quirksmode. A very handy resource that has taught me quite a bit. Its a bit messy and unformatted, but thats because I planned to spruce up the looks of things at a later time. Also, keep in mind that some of the code is written with troubleshooting in mind, like the alerts. For example, overFalse() has a setTimeout that will eventually execute closeMenu() but for now it executes giveAlert() so I can see if it fires after the timeout. It does not.

[CODE]<script language=”JavaScript” type=”text/javascript”>

function getObj(name)
{
if (document.getElementById)
{
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all)
{
this.obj = document.all[name];
this.style = document.all[name].style;
}
else if (document.layers)
{
this.obj = document.layers[name];
this.style = document.layers[name];
}
}

function findPosX(obj)
{
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}

function showMenu(MenuName, TitleName) {
mouseIsOver = true;
var menuObj = new getObj(MenuName);
var titleObj = new getObj(TitleName);
var newX = findPosX(titleObj.obj);
var newY = findPosY(titleObj.obj);
newY = newY + 15;
menuObj.style.top = newY + ‘px’;
menuObj.style.left = newX + ‘px’;
menuObj.style.display=’inline’;
mouseIsOver = true;
}

function overTrue() {
mouseIsOver = true;
clearTimeout(s);
}
function overFalse(MenuName) {
mouseIsOver = false;
var t=setTimeout(“giveAlert();”,5000);

}
function giveAlert() {
alert(‘MenuName’);
}
function closeMenu(MenuName) {
mouseOver = true;
if (mouseIsOver == false) {
var menuObj = new getObj(MenuName);
menuObj.style.display=’none’;
}
else {
}
}

</script>

<span ID=”tiManage” onMouseOver=”showMenu(‘nmManage’,’tiManage’)”>Manage</span>

<div ID=”nmManage” style=”display: none; background-color: black;position:absolute;” onmouseover=”overTrue()” onMouseOut=”overFalse(‘nmManage’)”>
Here is where the testing stuff goes. Hoooray.
</div>[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@cyber1Dec 05.2005 — It would be way more easy to see where the problem is if you would post the whole code or at least an example file complete with all HTML page formatting so that anybody who wants to help you can just cut and paste and try it on their browser.

Better yet post a link to a test page or attach a file.

With that large of a snippet it might as well be greek.

-Bill
Copy linkTweet thisAlerts:
@AaronHethauthorDec 05.2005 — Well, that is all of it. I start small and work my way up.

An example can be seen at http://rev.shadonet.com/progress.html or just as easily can be saved from the code given.

It seems that the setTimeout works correctly now, but not when it executes closeMenu(), and the alert pops up an insane amount of times.
Copy linkTweet thisAlerts:
@cyber1Dec 05.2005 — Well, that is all of it. I start small and work my way up.

An example can be seen at http://rev.shadonet.com/progress.html or just as easily can be saved from the code given.

It seems that the setTimeout works correctly now, but not when it executes closeMenu(), and the alert pops up an insane amount of times.[/QUOTE]


Thats what we don't know are we looking at the problem code or the whole thing.

Anyways just my opinion it looks way over complicated and I know you said thats not what you wanted and yes too complicated for me to try and figure out. If someone else wants to I'm sure they will.

What I would like to is point you to one of the simplest, smallest that I have ever seen.

[URL=http://css.alsacreations.com/Construction-de-menus-en-CSS/Un-menu-deroulant-en-CSS-et-XHTML-vertical-et-horizontal]http://css.alsacreations.com/Construction-de-menus-en-CSS/Un-menu-deroulant-en-CSS-et-XHTML-vertical-et-horizontal.[/URL]

and if that doesn't do anything for ya try this one:

[URL=http://css.maxdesign.com.au/listamatic/index.htm]http://css.maxdesign.com.au/listamatic/index.htm[/URL]


-Bill
×

Success!

Help @AaronHeth spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...