2 Javascript中的setTimeout无法正常工作
我设法创建了第一个隐藏和显示div的函数,但当我尝试使用2 setTime out时,另一个div正在无限加载,我需要做的是在不同的时间集中显示div,知道吗?2 Javascript中的setTimeout无法正常工作,javascript,jquery,Javascript,Jquery,我设法创建了第一个隐藏和显示div的函数,但当我尝试使用2 setTime out时,另一个div正在无限加载,我需要做的是在不同的时间集中显示div,知道吗? 这是我的代码 function showDiv1() { // If there are hidden divs left if ($('.forHide:hidden').length) { // Fade the first of them in $('.forHide:hidden:first').fade
这是我的代码
function showDiv1() {
// If there are hidden divs left
if ($('.forHide:hidden').length) {
// Fade the first of them in
$('.forHide:hidden:first').fadeIn();
if ($('.forHide:hidden').length >= 1) {
$(".forHide").fadeOut(1500, function() {});
}
// And wait one second before fading in the next one
myVar = setTimeout(showDiv1, 100);
}
}
function showDiv2() {
// If there are hidden divs left
if ($('.forSlowMo:hidden').length) {
// Fade the first of them in
$('.forSlowMo:hidden:first').fadeIn();
if ($('.forSlowMo:hidden').length >= 1) {
$(".forSlowMo").fadeOut(1500, function() {});
}
// And wait one second before fading in the next one
setTimeout(showDiv2, 1000);
}
}
在第一项的
.fadeIn
之后,如果还有任何隐藏项,则代码当前会将其全部淡出:
$('.forSlowMo:hidden:first').fadeIn();
if ($('.forSlowMo:hidden').length >= 1) {
// this line selects all, including the one just shown
$(".forSlowMo").fadeOut(1500, function() {});
}
您需要排除刚才显示的内容:
var x = $('.forSlowMo:hidden:first').fadeIn();
if ($('.forSlowMo:hidden').length >= 1) {
$(".forSlowMo").not(x).fadeOut(1500, function() {});
}
更新小提琴:
在小提琴中,我删除了.forHide部分,以便您可以看到.slowMo上的差异,因为它们彼此重叠。在
showDiv2
中,什么条件应该停止递归?setTimeout(showDiv1,100)
不会等待1秒,它等待0.1
秒。你说的另一个div无限加载是什么意思?我很难想象这一点。setTimeout
只有在它隐藏时才被调用,'fadeIn()'使它不隐藏=没有无限循环这里是我的JSFIDLE