jQuery(function($) { var status = null; var timeout = null; var menu = $('.menu-item-has-children'); var overlay = $('.body-overlay'); var over = function () { if (timeout) { clearTimeout(timeout); timeout = null; } if (status == 'over') { return; } overlay.show(); overlay.animate ({ opacity: 1 }, 100 ); status = 'over'; }; var out = function () { timeout = setTimeout(function() { timeout = null; overlay.animate ({ opacity: 0 }, 100, function () { overlay.hide(); status = 'out'; } ); }, 100); }; menu.hover(over, out); });