简体   繁体   English

延迟后JS setTimeout获取事件

[英]JS setTimeout get event after delay

I want to translate values if the user is hovering over the element with the mouse for 1.5 seconds. 如果用户使用鼠标悬停在元素上1.5秒,我想翻译值。 I wrote a little jQuery function, but I don't know how I can verify if the user is over the element after 1.5 seconds. 我写了一个小jQuery函数,但我不知道如何在1.5秒后验证用户是否在元素上。

$(document).on('mouseover', '.search-translate', function(e) {
    setTimeout(function() { translate(e, this); }, 1500);
});

function translate(pEvent, pThis)
{
    if(pEvent.currentTarget == ???.currentTarget)
    {
        $.ajax(.....);
    }
}

Does anybody have an idea? 有人有想法吗?

Use this snippet of code.... 使用这段代码....

 $(document).on('mouseover', '.search-translate', function(e) { var obj=this; setTimeout(function() { translate(e, obj); }, 1500); }); function translate(pEvent, pThis) { alert(pEvent+" : "+pThis); } 

If I mouseover the setTimeout start, if I mouseout the setTimeout event will be canceled. 如果我鼠标悬停在setTimeout开始,如果我mouseout,setTimeout事件将被取消。

var tTimeout;

function translate(pEl)
{
    ....    
}

$(document).ready(function() {  
    $(document).on('mouseover', '.search-translate', function(e) {
        var el = this;

        tTimeout = setTimeout(function() { translate(el); }, 1500);
});

$(document).on('mouseout', '.search-translate', function() {
    clearTimeout(tTimeout);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM