views:

118

answers:

2

I am trying to make a simple setTimeout, to make a div tag invisible after 2 seconds.

The setTimeout function makes the div invisible but irregularly, sometimes immediately and sometimes after 1 sec, and so on.

Here is my code:

  function beginTimeOut(){

    t = setTimeout(function(){hideSubMenu()},2000);

}

function hideSubMenu(){
    var elem;
    elem = document.getElementById("ul_navlist1");
    elem.style.visibility="hidden";
    clearTimeout(t);

}

By the way, t is a global variable. I have tried this too: t = setTimeout("hideSubMenu()",2000); but with the same irregular results.

UPDATE from the OP:

This is the div that contains my menu and submenu. I edited it to be a little bit readable here.

The right_rect is the div that contains the menu and submenu. In this div I call onmouseout to hide the submenu.

<div class="right_rect"  onmouseout="beginTimeOut();">      
        <div class="menu2" >
            <ul >
            <li onclick="hideOrShow();"><a href="#">item1</a></li>
                </ul>
        </div>

    <div id="ul_navlist1">
        <ul >
                    <li><a href="#">sub_item1</a></li>
            </ul>
        </div>      
</div>

and this is the part of the javascript that I use to perform the hiding and showing process.

function hideOrShow(){
    var hOrV;
    var elem;
    var styleType = "visibility";
    elem = document.getElementById("ul_navlist1");
    hOrV = getStyle(elem, styleType);
    if(hOrV =="hidden"){
        //alert();
        elem.style.visibility="visible";
    }else{
        elem.style.visibility="hidden";
    }
}

function beginTimeOut(){
    setTimeout(function(){document.getElementById("ul_navlist1").style.visibility="hidden";}, 2000);

}

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

The getStyle function is not my code.

+1  A: 

You can do this:

function beginTimeOut(){
  setTimeout(hideSubMenu,2000);
}

function hideSubMenu(){
  document.getElementById("ul_navlist1").style.visibility="hidden";
}

setTimeout() only runs once, no need to clear it unless you want to stop it from executing before it does. I'm not sure from your question where you're calling beginTimeOut(), but the time at which it's kicked off may vary. I'd do this in a document load event of some sort or at the end of your <body>, depends what else you have running.

As a side note, you could put this in a single statement, like this:

setTimeout(function() {
  document.getElementById("ul_navlist1").style.visibility="hidden";
}, 2000);
Nick Craver
Thank you Nick for your answer. Unfortunately it behaves like before.I am trying to make a submenu, using ul, that gets invisible when the mouse leaves the div that contains the ul. I dont know if it is the right way to do things but I use onmouseout to make the submenu invisible. Any suggestions? thank you.
mnish
@mnish - Can you post your code that's doing this? It's hard to say exactly without knowing exactly where you're calling the methods.
Nick Craver
@Nick Craver - Could you please check my code and tell me what is wrong? Check my original message there I edited my post and now it includes my code. thank you
mnish
A: 

Because you are clearing the timeout by using a global variable, there's no guarantee that it will be the right timeout that gets cleared. What if you call beginTimeout before the previous timeout completes? As said by Nick Craver, you probably don't need to clear the timeout.

A coding mistake I've seen more than once is code that uses setInterval/setTimeout and the handle gets overwritten, and the interval persists unexpectedly. It's best to capture the interval when it's created using this fairly obtuse code:

var t=setInterval(function(){doStuff(t)},1000)

So that the handle of the firing interval is passed to the handler. This avoids the problem of storing it globally.

spender
Thank you for your answer. ehm, I dont think I understand much of what you mean, sorry. Maybe because that I am just a beginner but setTimeOut is really confusing!
mnish