简体   繁体   English

悬停后jQuery延迟不起作用

[英]Jquery delay is not working after hover

I have problem with jQuery delay. 我对jQuery延迟有疑问。 I need to add a class after 5 seconds, so I wrote this: 我需要在5秒后添加一个类,所以我这样写:

Working example: 工作示例:

$('#category #product_list .ajax_block_product').on('mouseover',function(){
    var that = $(this);
    setTimeout(function(){
        that.find('.sl').show();
        that.find('.product_img_link img').hide();
        that.addClass('hovered');
    }, 500);
});

$('#category #product_list .ajax_block_product').on('mouseleave',function(){
    $(this).find('.sl').hide();
    $(this).find('.product_img_link img').show();
    $(this).removeClass('hovered');
}); 

It is working on the first time, but when I hover a second time, the hovered class is not added and I do not know why. 它是第一次工作,但是当我第二次悬停时,没有添加hovered类,我也不知道为什么。

Can someone help me with this problem? 有人可以帮我解决这个问题吗?

Try to use; 尝试使用;

var tm;
$('#category #product_list .ajax_block_product').on('mouseover',function(){
    var that = $(this);
    tm = setTimeout(function(){
        that.find('.sl').show();
        that.find('.product_img_link img').hide();
        that.addClass('hovered');
    }, 500);
});

$('#category #product_list .ajax_block_product').on('mouseleave',function(){
    clearTimeout(tm);
    that.find('.sl').hide();
    that.find('.product_img_link img').show();
    that.removeClass('hovered');
}); 

You need to change the 500 to 5000, as mentioned in the comments. 如注释中所述,您需要将500更改为5000。 100 = 100ms (millisecond), not 1 second. 100 = 100ms(毫秒),而不是1秒。 1 second = 1000ms. 1秒= 1000ms。

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

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