在fadeOut之前的JQuery延迟

我写了一个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); } 

你可以写

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

试试这个

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

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

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

我认为问题是你必须传递一个函数作为第一个参数,并且你传递一个字符串。 尝试用这个替换你的setTimeout()行:

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

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