在fadeOut之前的JQuery延迟

时间:2013-03-28 16:01:27

标签: jquery delay

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

这是我写的脚本:

$(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);
}

4 个答案:

答案 0 :(得分:36)

你可以写

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

答案 1 :(得分:3)

试试这个

$(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++;
}

答案 2 :(得分:3)

你试过.delay()吗? 类似的东西:

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

答案 3 :(得分:1)

我认为问题在于您必须将函数作为第一个参数传递,并且您要传递一个字符串。请尝试将此setTimeout()行替换为:

setTimeout(function(){
    ShowPostDiv(divIndex);
}, 4000);

来源: http://www.w3schools.com/jsref/met_win_settimeout.asp

相关问题