繁体   English   中英

2 Javascript中的setTimeout无法正常工作

[英]2 setTimeout in Javascript not working properly

我设法创建了第一个隐藏和显示div的函数,但是当我尝试使用2 setTime输出另一个div无限加载时,我需要做的是在不同的时间集中显示div,有什么想法吗?
这是JS我的代码

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

更新的小提琴: https : //jsfiddle.net/hd8q1mox/1/

在小提琴中,我删除了.forHide部分,以便您可以看到.slowMo上的差异,因为它们彼此重叠。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM