Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the code robust against duplicated timeouts #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions bootstrap-hover-dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
// shownEvent = 'shown.bs.dropdown',
// hiddenEvent = 'hidden.bs.dropdown',
settings = $.extend(true, {}, defaults, options, data),
timeout, timeoutHover;
timeout, timeoutHover,
timeouts = [],
timeoutsHover = [];

$parent.hover(function (event) {
// so a neighbor can't open the dropdown
Expand All @@ -56,12 +58,12 @@
openDropdown(event);
}, function () {
// clear timer for hover event
window.clearTimeout(timeoutHover)
timeout = window.setTimeout(function () {
while(timeout = timeoutsHover.pop()) { window.clearTimeout(timeout) };
timeouts.push(window.setTimeout(function () {
$this.attr('aria-expanded', 'false');
$parent.removeClass('open');
$this.trigger(hideEvent);
}, settings.delay);
}, settings.delay));
});

// this helps with button groups!
Expand Down Expand Up @@ -102,23 +104,23 @@
}

// clear dropdown timeout here so it doesnt close before it should
window.clearTimeout(timeout);
while(timeout = timeouts.pop()) { window.clearTimeout(timeout) };
// restart hover timer
window.clearTimeout(timeoutHover);
while(timeout = timeoutsHover.pop()) { window.clearTimeout(timeout) };

// delay for hover event.
timeoutHover = window.setTimeout(function () {
timeoutsHover.push(window.setTimeout(function () {
$allDropdowns.find(':focus').blur();

if(settings.instantlyCloseOthers === true)
$allDropdowns.removeClass('open');

// clear timer for hover event
window.clearTimeout(timeoutHover);
while(timeout = timeoutsHover.pop()) { window.clearTimeout(timeout) };
$this.attr('aria-expanded', 'true');
$parent.addClass('open');
$this.trigger(showEvent);
}, settings.hoverDelay);
}, settings.hoverDelay));
}
});
};
Expand Down