我有一个菜单,当我使用jQuery进行鼠标悬停时会打开。
当我单击每个单独的链接时,它也会打开。
问题是我想确保仅在访问者至少在链接上停留两秒钟或单击链接时才触发。
这就是我所拥有的:
// Open the menu on mouse hover too
// so simulate a click when aria-expanded == true
var timeoutId;
$("#main-header #menu-row nav a").hover(function() {
if (!timeoutId) {
timeoutId = window.setTimeout(function() {
timeoutId = null; // EDIT: added this line
$(this).trigger('click');
}, 500);
}
},
function () {
if (timeoutId) {
window.clearTimeout(timeoutId);
timeoutId = null;
}
else {
if($(this).attr('aria-expanded') == 'true') {
} else {
$(this).trigger('click');
}
}
});
现在,问题在于:
那就是问题!
我现在要发布完整的工作代码。感谢下面的提示,我解决了! (请注意在超时后 => )
var timeoutId;
$("#main-header #menu-row nav a").hover(function() {
if (!timeoutId) {
timeoutId = window.setTimeout(() => { // NOTE THIS LINE!
timeoutId = null; // EDIT: added this line
$(this).trigger('click');
}, 300);
}
},
function () {
if (timeoutId) {
window.clearTimeout(timeoutId);
timeoutId = null;
}
else {
if($(this).attr('aria-expanded') == 'true') {
} else {
$(this).trigger('click');
}
}
});