如何延迟(“mouseenter”)2秒后延迟?

时间:2014-01-09 05:10:55

标签: javascript jquery mouseevent onmouseover

这是我的代码。

在第4行,我想在鼠标输入lastItemsLink 2秒后运行this.openList。

我该怎么做?

lastItemsLink = $(".last-items"),
openLastItemsList = {
    init: function() {
        lastItemsLink.on("mouseenter", this.openList);
        lastItemsLink.on("mouseleave", this.closeList);
    },
    openList: function() {
        lastItemsContainer.stop(false, true).slideDown("fast");
    },
    closeList: function() {
        lastItemsContainer.stop(false, true).fadeOut("fast");
    }
};

3 个答案:

答案 0 :(得分:2)

您可能需要执行类似

的操作
lastItemsLink = $(".last-items"),
openLastItemsList = {
    init: function () {
        lastItemsLink.on("mouseenter", this.openList);
        lastItemsLink.on("mouseleave", this.closeList);
    },
    openList: function () {
        openLastItemsList.timer = setTimeout(function () {
            lastItemsContainer.stop(false, true).slideDown("fast");
            delete openLastItemsList.timer;
        }, 2000)
    },
    closeList: function () {
        if (openLastItemsList.timer) {
            clearTimeout(openLastItemsList.timer)
        } else {
            lastItemsContainer.stop(false, true).fadeOut("fast");
        }
    }
};

演示:Fiddle

答案 1 :(得分:1)

使用setTimeout

lastItemsLink.on("mouseenter", function () {
    setTimeout(this.openList, 2000)
});

答案 2 :(得分:1)

尝试.delay()函数 click here

this.delay(2000).openList

可能会尝试一下......