So jQuery and setTimeOut
is eluding me! and I appeal to the kind nature of the Stack community for help.
I am creating a bespoke drop-down navigation and having a few issues implementing setTimeOut
to the script so that the menu has time-out in its response.
I shall go into more detail regarding the time-outs.
The menu needs to have a time-out on mouseenter
so the user can accidentally hover over a link and not trigger the drop-down; currently that is set to 400ms. I also want a time-out that allows the user to mouseleave
the drop down and have 1000ms to hover back to the drop down before the drop down hides.
The caveat of this is that putting the time-outs in stop the drop-down from seamlessly switching between drop-downs with no flickering.
If one drop-down is visible and the user mouses to another top level link (parent, i.e. "services") the visual of the drop-down (its container) shows and only the data within the drop-down changes.
I hope this covers the requirements of the script, if not please ask me to elaborate.
JSfiddle link: http://jsfiddle.net/ATqqv/8/
And for purists or peeps who cannot visit the JSfiddle site here is my JS code.
function responsive_navigation() {
var hover, $this;
$target = $("header nav > ul > li");
$target.on('mouseenter click', function (e) {
$this = $(this);
if(hover) {
clearTimeout(hover); // Cancel the timeout to hide the menu
hover = null;
}
hover = setTimeout(nav_show, 400);
}).on('mouseleave', function () {
//clearTimeout(hover);
//hover = setTimeout(nav_hide, 1000);
nav_hide();
});
// Show the menu relative to user actions
function nav_show() {
$this.addClass('active');
$this.children('.dropnav').show();
}
// Hide the menu relative to user actions
function nav_hide() {
$this.children('.dropnav').hide();
$this.removeClass('active');
}
}
$(document).ready(function () {
responsive_navigation();
});
Thank you in advance any and all that help and contribute to an answer.