setTimeout is acting weird
        Posted  
        
            by mnish
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by mnish
        
        
        
        Published on 2010-04-28T23:28:05Z
        Indexed on 
            2010/04/28
            23:37 UTC
        
        
        Read the original article
        Hit count: 236
        
JavaScript
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.
© Stack Overflow or respective owner