Javascript 基于超时有条件地调用jquery事件

Javascript 基于超时有条件地调用jquery事件,javascript,jquery,Javascript,Jquery,我有一个屏幕,其中有许多链接和对他们的悬停,我需要显示一些信息的工具提示div。 我的工具提示运行良好。但是有一个测试抱怨说,工具提示甚至出现在悬停在链接上(当意图是看不到工具提示时),这很烦人。 我的新要求是:工具提示只有在用户悬停5秒时才可见(例如)。 有人能告诉我怎么做吗? 我读过关于settimeout()的内容,但我认为这只会延迟函数调用。我需要的是中止函数调用,以防用户在5秒钟内不悬停。 下面是我的代码: $(document) .ready(

我有一个屏幕,其中有许多链接和对他们的悬停,我需要显示一些信息的工具提示div。 我的工具提示运行良好。但是有一个测试抱怨说,工具提示甚至出现在悬停在链接上(当意图是看不到工具提示时),这很烦人。 我的新要求是:工具提示只有在用户悬停5秒时才可见(例如)。 有人能告诉我怎么做吗? 我读过关于settimeout()的内容,但我认为这只会延迟函数调用。我需要的是中止函数调用,以防用户在5秒钟内不悬停。 下面是我的代码:

$(document)
                .ready(
                        function() {

                            var changeTooltipPosition = function(event) {
                                var tooltipX = event.pageX - 8;
                                var tooltipY = event.pageY + 8;
                                $('div.tooltip').css({
                                    top : tooltipY,
                                    left : tooltipX
                                });
                            };

                            var showTooltip = function(event) {
                                //alert($(this).val());
                                //var hiddenProductData = $(this).closest('tr').find('input:hidden').val(), 

                                var hiddenProductData = "",
                                    tableString = "<div class='tooltip' ><table><thead></thead><tbody>";
                                var hiddenModuleType = $('#hiddenModuleType').val(),
                                    hiddenid         = $(this).closest('tr').find('.id').val(); 
                                var hiddenProductDataTokens, nameValueTokens;
                                //$.getJSON('/getToolTipInfo/'+hiddenModuleType+'/'+hiddenid, function(jsonData) {

                                if (hiddenProductData
                                                    && hiddenProductData != '') {

                                }

                                $.getJSON('getToolTipInfo/'+hiddenModuleType+'/'+hiddenid, function(jsonData) {

                                            hiddenProductData = jsonData.data;
                                            if (hiddenProductData
                                                    && hiddenProductData != '') {
                                                hiddenProductDataTokens = hiddenProductData
                                                        .split("#");
                                                if (hiddenProductDataTokens.length > 0) {
                                                    $
                                                            .each(
                                                                    hiddenProductDataTokens,
                                                                    function(index,
                                                                            value) {
                                                                        nameValueTokens = value
                                                                                .split(",");
                                                                        tableString += "<tr><td>"
                                                                                + nameValueTokens[0]
                                                                                + " : "
                                                                                + nameValueTokens[1]
                                                                                + "</td></tr>";
                                                                    });
                                                }
                                                tableString += "</tbody></table></div>";
                                            }
                                            $(tableString).appendTo('body');
                                            changeTooltipPosition(event);
                                });


                                /* $(tableString).appendTo('body');
                                changeTooltipPosition(event); */

                            };

                            var hideTooltip = function() {
                                $('div.tooltip').remove();
                            };

//I want to be able to abort the showtooltip on mouseenter in case hover is for less     than 5 seconds
                             $(".tooltipelement").bind({
                                mousemove : changeTooltipPosition,
                                mouseenter : showTooltip,
                                mouseleave : hideTooltip
                            }); 
                        });
$(文档)
.准备好了吗(
函数(){
var changeTooltipPosition=函数(事件){
var tooltipX=event.pageX-8;
var tooltipY=event.pageY+8;
$('div.tooltip').css({
顶部:工具提示,
左:工具提示
});
};
var showTooltip=函数(事件){
//警报($(this.val());
//var hiddenProductData=$(this).closest('tr').find('input:hidden').val(),
var hiddenProductData=“”,
表字符串=”;
var hiddenModuleType=$('#hiddenModuleType').val(),
hiddenid=$(this.closest('tr').find('.id').val();
var hiddenProductDataTokens、nameValueTokens;
//$.getJSON('/getToolTipInfo/'+hiddenModuleType+'/'+hiddenid,函数(jsonData){
if(hiddenProductData)
&&hiddenProductData!=“”){
}
$.getJSON('getToolTipInfo/'+hiddenModuleType+'/'+hiddenid,函数(jsonData){
hiddenProductData=jsonData.data;
if(hiddenProductData)
&&hiddenProductData!=“”){
hiddenProductDataTokens=hiddenProductData
.拆分(“#”);
如果(hiddenProductDataTokens.length>0){
$
.每个(
hiddenProductDataTokens,
功能(索引,
价值){
nameValueTokens=value
.拆分(“,”);
表字符串+=“”
+nameValueTokens[0]
+ " : "
+nameValueTokens[1]
+ "";
});
}
表字符串+=“”;
}
$(表字符串).appendTo('body');
更改工具位置(事件);
});
/*$(表字符串).appendTo('body');
更改工具位置(事件)*/
};
var hideTooltip=函数(){
$('div.tooltip').remove();
};
//我希望能够中止鼠标指针上的showtooltip,以防悬停时间少于5秒
$(“.ToolTipeElement”).bind({
鼠标移动:changeTooltipPosition,
鼠标指针:显示工具提示,
鼠疫
}); 
});
谢谢
Suvo

您确实可以将setTimeout与cleartimout结合使用

在MoseOver事件中,调用setTimeout并将其引用保存在一些变量中,如下所示:

var show_tooltip_timeout = setTimeout(<your function>, delay);
var hoverDelayTimeout = null; // remember the timer id in this variable

$(".tooltipelement").bind({
    mousemove : changeTooltipPosition,
    mouseenter : function(event) {
        // start a timer that calls showTooltip after 5 secs and store it's ID
        hoverDelayTimeout = setTimeout(function() {
            showTooltip(event);
        }, 5000);
    },
    mouseleave : function(event) {
        // clear the current timer (will prevent the tooltip if less than 5 secs have passed)
        clearTimeout(hoverDelayTimeout);
        hideTooltip();
    }
});

您确实可以将setTimeout与clearTimeout结合使用

在MoseOver事件中,调用setTimeout并将其引用保存在一些变量中,如下所示:

var show_tooltip_timeout = setTimeout(<your function>, delay);
var hoverDelayTimeout = null; // remember the timer id in this variable

$(".tooltipelement").bind({
    mousemove : changeTooltipPosition,
    mouseenter : function(event) {
        // start a timer that calls showTooltip after 5 secs and store it's ID
        hoverDelayTimeout = setTimeout(function() {
            showTooltip(event);
        }, 5000);
    },
    mouseleave : function(event) {
        // clear the current timer (will prevent the tooltip if less than 5 secs have passed)
        clearTimeout(hoverDelayTimeout);
        hideTooltip();
    }
});

您确实可以将setTimeout与clearTimeout结合使用

在MoseOver事件中,调用setTimeout并将其引用保存在一些变量中,如下所示:

var show_tooltip_timeout = setTimeout(<your function>, delay);
var hoverDelayTimeout = null; // remember the timer id in this variable

$(".tooltipelement").bind({
    mousemove : changeTooltipPosition,
    mouseenter : function(event) {
        // start a timer that calls showTooltip after 5 secs and store it's ID
        hoverDelayTimeout = setTimeout(function() {
            showTooltip(event);
        }, 5000);
    },
    mouseleave : function(event) {
        // clear the current timer (will prevent the tooltip if less than 5 secs have passed)
        clearTimeout(hoverDelayTimeout);
        hideTooltip();
    }
});

您确实可以将setTimeout与clearTimeout结合使用

在MoseOver事件中,调用setTimeout并将其引用保存在一些变量中,如下所示:

var show_tooltip_timeout = setTimeout(<your function>, delay);
var hoverDelayTimeout = null; // remember the timer id in this variable

$(".tooltipelement").bind({
    mousemove : changeTooltipPosition,
    mouseenter : function(event) {
        // start a timer that calls showTooltip after 5 secs and store it's ID
        hoverDelayTimeout = setTimeout(function() {
            showTooltip(event);
        }, 5000);
    },
    mouseleave : function(event) {
        // clear the current timer (will prevent the tooltip if less than 5 secs have passed)
        clearTimeout(hoverDelayTimeout);
        hideTooltip();
    }
});

您可以这样做:

var show_tooltip_timeout = setTimeout(<your function>, delay);
var hoverDelayTimeout = null; // remember the timer id in this variable

$(".tooltipelement").bind({
    mousemove : changeTooltipPosition,
    mouseenter : function(event) {
        // start a timer that calls showTooltip after 5 secs and store it's ID
        hoverDelayTimeout = setTimeout(function() {
            showTooltip(event);
        }, 5000);
    },
    mouseleave : function(event) {
        // clear the current timer (will prevent the tooltip if less than 5 secs have passed)
        clearTimeout(hoverDelayTimeout);
        hideTooltip();
    }
});

请注意mouseenter上的函数嵌套,这是将事件传递到showTooltip所必需的。如果您在某个时候也需要hideTooltip中的事件,则必须以相同的方式包装它。

您可以这样做:

var show_tooltip_timeout = setTimeout(<your function>, delay);
var hoverDelayTimeout = null; // remember the timer id in this variable

$(".tooltipelement").bind({
    mousemove : changeTooltipPosition,
    mouseenter : function(event) {
        // start a timer that calls showTooltip after 5 secs and store it's ID
        hoverDelayTimeout = setTimeout(function() {
            showTooltip(event);
        }, 5000);
    },
    mouseleave : function(event) {
        // clear the current timer (will prevent the tooltip if less than 5 secs have passed)
        clearTimeout(hoverDelayTimeout);
        hideTooltip();
    }
});

请注意mouseenter上的函数嵌套,这是将事件传递到showTooltip所必需的。如果您在某个时候也需要hideTooltip中的事件,则必须以相同的方式包装它。

您可以这样做:

var show_tooltip_timeout = setTimeout(<your function>, delay);
var hoverDelayTimeout = null; // remember the timer id in this variable

$(".tooltipelement").bind({
    mousemove : changeTooltipPosition,
    mouseenter : function(event) {
        // start a timer that calls showTooltip after 5 secs and store it's ID
        hoverDelayTimeout = setTimeout(function() {
            showTooltip(event);
        }, 5000);
    },
    mouseleave : function(event) {
        // clear the current timer (will prevent the tooltip if less than 5 secs have passed)
        clearTimeout(hoverDelayTimeout);
        hideTooltip();
    }
});
不是