如何让 jQuery 在关闭滑动菜单之前等待一秒钟?

How can I get jQuery to wait a second before closing my sliding menu?

我尝试使用 setTimeout,但它只是破坏了一些东西。这是我的代码:

$(document).ready(function() {
    $('.menu-item-has-children').hover(
        function() {
            $(this).children('.sub-menu').slideDown(400);
        },
        function() {
            $(this).children('.sub-menu').slideUp(400);
        }
    );
}); // end ready

我想在 .slideUp 之前暂停 1 秒。帮忙?

您可以使用 delay:

Set a timer to delay execution of subsequent items in the queue.

$(document).ready(function() {
    $('.menu-item-has-children').hover(
        function() {
            $(this).children('.sub-menu').delay(1000).slideDown(400);
        },
        function() {
            $(this).children('.sub-menu').delay(1000).slideUp(400);
        }
    );
}); // end ready

文档:https://api.jquery.com/delay/

使用setTimeout函数:

setTimeout(function() {
   $(this).children('.sub-menu').slideUp(400);
}, 1000);