simple jquery dropdown - clearTimeout, setTimeout issues
        Posted  
        
            by user210757
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user210757
        
        
        
        Published on 2010-02-23T05:01:27Z
        Indexed on 
            2010/06/05
            11:22 UTC
        
        
        Read the original article
        Hit count: 448
        
HTML:
<ul class="topnav">
    <li><a href="#"><span>One</span></a></li>
    <li><a href="#"><span>Two</span></a></li>
    <li>
        <li><a href="#"><span>Three</span></a></li>
        <ul class="subnav">
            <li><a href="#">A</a></li>
            <li><a href="#">B</a></li>
            <li><a href="#">C</a></li>
        </ul>
    </li>
</ul>
jquery:
var timeout = null;
$(document).ready(function() {
    $("ul.topnav li").mouseover(function() {
        if (timeout) clearTimeout(timeout);
        $(this).find("ul.subnav").slideDown('fast').show();
    }).mouseout(function() {
        timeout = setTimeout(closemenu, 500);
    });
    // sub menu mouseovers keep dropdown open
    $("ul.subnav li").mouseover(function() {
        if (timeout) clearTimeout(timeout);
    }
    ).mouseout(function() {
        timeout = setTimeout(closemenu, 500);
        // alert(timeout);
    });
    // any click closes
    $(document).click(closemenu);
});
// Closes all open menus 
function closemenu() {
    $('ul.subnav:visible').hide();
    if (timeout) clearTimeout(timeout);
} 
I'm having issues with timeout. In use, if i mouseover "Three", the dropdown stays out forever. if i mouseover "A", dropdown will stay out forever, but if I mouseover "B" or anything lower, the menu will close on me. if you uncomment "// alert(timeout);" it gets there for B, (and A) but timeout will have a value. why is this? i thought clearTimeout would null the timeout variable?
© Stack Overflow or respective owner