当我更改一些文本时,我需要延迟

I need a delay when I change some text

本文关键字:延迟 文本      更新时间:2023-09-26

我需要一个提示!我想改变一些文本与延迟在它。对于.delay,它不起作用,因为它不是队列。然后我用.setTimeout试了试。这是有效的,但只适用于一个文本框。当我再加一个时,它就不能正常工作了。有人能帮我吗?

代码如下:

$('#text-box1').hover(function() {
     $('#text-box1').text("The text changed!");
}, function() {
     $('#text-box1').text("The previous Text");
});

您需要知道是否有超时工作,并首先停止它。这是你的解决方案:

$(function(){
    var to;
    var $tb1 = $('#text-box1');
    $tb1.on('mouseenter', function(){
        if( to !== undefined ) return;
        var $this = $(this);
        to = setTimeout(function(){
            $this.text('The text changed!');
            to = undefined;
        }, 500);
    }).on('mouseleave', function(){
        if( to !== undefined ) return;
        var $this = $(this);
        to = setTimeout(function(){
            $this.text('The previous Text');
            to = undefined;
        }, 500);
    });
});

检查jsFiddle