简体   繁体   English

Javascript setInterval需要太长时间才能重置

[英]Javascript setInterval taking too long to reset

Hey I have a panel (br_Panel) that contains four divs with the class 'smallPanel' and id br_Panel1, br_Panel2, etc that are of the same and equal size and positioned overlapping each other. 嘿,我有一个面板(br_Panel),其中包含四个div,其类为'smallPanel',标识为br_Panel1,br_Panel2等,它们的大小相同且相等,并且彼此重叠。 When the function runs, every 5 seconds one fades out and shows the one below, and when they have all faded out they all come back with the fadeIn. 该功能运行时,每5秒钟就会淡出一个,并在下面显示一个,当它们全部淡出时,它们都将全部使用fadeIn返回。 The problem is the pause between the last div's fadeout and the fadein for all of them is 15 seconds, three times as long as it takes each div to leave. 问题在于最后一个div的淡入和淡入之间的暂停时间为15秒,是每个div离开所需时间的三倍。 How can I reduce this pause in reset time to 5 seconds? 如何将重置时间的暂停时间减少到5秒?

setInterval(function() {
    if(i < 0) {
        $('#br_Panel').find($('.smallPanel')).fadeIn();
        i = 5;
    }
    else
        i--;
    $('#br_Panel').find($('#br_Panel' + i)).fadeOut();
}, 5000);

This is the html (if it helps, each of the innermost divs is positioned absolute to #br_Panel so that they overlap each other): 这是html(如果有帮助,则最里面的div绝对位于#br_Panel的绝对位置,以使它们彼此重叠):

<div class="height1 panel" id="br_Panel">
    <div class="smallPanel" id="br_Panel1">content</div>
    <div class="smallPanel" id="br_Panel2">content</div>
    <div class="smallPanel" id="br_Panel3">content</div>
    <div class="smallPanel" id="br_Panel4">content</div>
</div>

You mentioned that your br_Panel contains four divs with the class smallPanel, but your interval function will run six times before i is reset (5, 4, 3, 2, 1, 0). 您提到您的br_Panel包含带有smallPanel类的四个div,但是您的interval函数将在重置i之前运行六次(5、4、3、2、1、0)。 It may be that your function is running 2 more times than needed, which causes your delay to be 10 seconds longer than it should be. 可能是您的函数运行了比所需次数多2次的时间,这导致您的延迟时间比原先的时间长了10秒。

As an additional note, when using .find(), you need only pass in the string css selector you are using, not the enter jQuery object: 另外要注意的是,在使用.find()时,只需传入正在使用的字符串css选择器,而不需要输入jQuery对象:

$('#br_Panel').find('.smallPanel').fadeIn();

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

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