淡出之前的 JQuery 延迟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15686598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:35:39  来源:igfitidea点击:

JQuery delay before fadeOut

jquerydelay

提问by user2220474

I have written a jquery script that allows me to fade divs in and out, then repeat. The code works fine. However, when I try to add a delay (I want the div to stay up a few seconds before fading out), it does not work properly. I've tried adding the delay in several places inside the code and none seem to function properly. I am using Jquery version 1.9.1

我写了一个 jquery 脚本,它允许我淡入淡出 div,然后重复。代码工作正常。但是,当我尝试添加延迟(我希望 div 在淡出之前保持几秒钟)时,它无法正常工作。我尝试在代码中的几个地方添加延迟,但似乎没有一个功能正常。我正在使用 Jquery 1.9.1 版

Here is the script that I have written:

这是我写的脚本:

$(document).ready(function(){
   ShowPostDiv(0);
});

function ShowPostDiv(divIndex)
{
    $(".home_entry_txt").hide();

    if(divIndex >= $(".rotate_hide").length)
    {
        divIndex = 0;
    }
    var divPostHtml = $(".rotate_hide:eq("+divIndex+")").html();
    $(".home_entry_txt").html(divPostHtml); 
    $(".home_entry_txt").fadeIn(3000, function(){
             $(".home_entry_txt").fadeOut("slow");
        });
    divIndex++;
    setTimeout("ShowPostDiv("+divIndex+")", 4000);
}

回答by enb081

You can just write

你可以写

$(".home_entry_txt").fadeIn(3000).delay(1000).fadeOut("slow");

回答by iAmClownShoe

try this

尝试这个

$(document).ready(function(){
   ShowPostDiv(0);
});

function ShowPostDiv(divIndex)
{
    $(".home_entry_txt").hide();

    if(divIndex >= $(".rotate_hide").length)
    {
        divIndex = 0;
    }
    var divPostHtml = $(".rotate_hide:eq("+divIndex+")").html();
    $(".home_entry_txt").html(divPostHtml); 
    $(".home_entry_txt").fadeIn(3000, function(){
        setTimeout(function(){
            $(".home_entry_txt").fadeOut("slow");
        },4000);
    });
    divIndex++;
}

回答by kelly johnson

Have you tried .delay()? something like:

你试过 .delay() 吗?就像是:

$(".home_entry_txt").fadeIn().delay(200).queue(function(next) {
$(".home_entry_txt").fadeOut("slow");
});