简体   繁体   English

将setTimeout交换为setinterval,有人可以帮助我解决此问题吗

[英]Swapping setTimeout to setinterval, Can anyone help me solve this issue

Im currently in the middle of building test websites to help me understand coding more and try and get a better grasp of how it all comes together. 我目前正在建立测试网站,以帮助我更多地了解编码并尝试并更好地理解它们的组合方式。

My problem is this - I have a section of the site with a number of quotes, they fade in then slide up out of view revealing a new quote. 我的问题是-我在网站的一部分中引用了许多引号,这些引号会逐渐消失,然后滑出视图以显示新的引号。 This is were is I have made the mistake. 这是我犯了错误。 I have it set perfectly so that it will cycle through all the quotes...but then stop. 我已对其进行了完美设置,以便它将在所有引号中循环显示...但是随后停止。 I would love them to repeat. 我希望他们再说一遍。 I know my mistake is using the.setTimeout function instead of the setInterval code. 我知道我的错误是使用.setTimeout函数而不是setInterval代码。

My question is this, is there an easy solution to swapping the code, Its taken me a good while just to wrap my head around this lol 我的问题是,有没有简单的解决方案来交换代码,这使我受益匪浅,只是想把我的头缠起来

Here is the code for you to look at. 这是供您查看的代码。 Any help would be greatly appreciated. 任何帮助将不胜感激。

$(function(){
setTimeout(function()
    {
    $("#jobs_1").slideUp(800);
    }, 4000); 
});
$(function(){
setTimeout(function()
    {
    $("#jobs_2").fadeIn(400);
    }, 4800); 
}); 
$(function(){
setTimeout(function()
    {
    $("#jobs_2").slideUp(800);
    }, 8000); 
});
$(function(){
setTimeout(function()
    {
    $("#jobs_3").fadeIn(400);
    }, 8800); 
});     
$(function(){
setTimeout(function()
    {
    $("#jobs_3").slideUp(800);
    }, 12000); 
});
$(function(){
setTimeout(function()
    {
    $("#jobs_4").fadeIn(400);
    }, 12800); 
});
$(function(){
setTimeout(function()
    {
    $("#jobs_4").slideUp(800);
    }, 16000); 
});
$(function(){
setTimeout(function()
    {
    $("#jobs_1").fadeIn(400);
    }, 16800); 


});     

Many Thanks 非常感谢

This should help you. 这应该对您有帮助。 How it works? 这个怎么运作? You set the current job counter to 0. Then, using setInterval, every 3 seconds you run the main function, which increments the job counter. 您将当前作业计数器设置为0。然后,使用setInterval,每3秒钟运行一次主函数,这会使作业计数器递增。 If the counter exceeds 4 (your last job), hide job 4 and fade int he first one. 如果计数器超过4(您的最后一份工作),则隐藏工作4并逐渐淡入第一个工作。 Otherwise slide out the previous job and fade in the new one. 否则,请滑出上一个作业,然后淡入新作业。

$(function(){
    count = 1;

    x = setInterval(function(){
        count++;
        if( count > 4) {
            $('jobs_4').hide(function(){
                $('#jobs_1').fadeIn();
            });

            count = 1
        } else {
            $('#jobs_' + (count-1)).slideUp(function(){
                $('#jobs_' + (count-1)).fadeIn();
            });
        }

    }, 3000);
}

You can move all your block names into a stack and use one single function: 您可以将所有块名称移动到堆栈中并使用一个功能:

var blocks = ['jobs_1', 'jobs_2', 'jobs_3', 'jobs_4'];
var curentBlockIndex = 0;
setInterval(function(){
    var nextBlockIndex = curentBlockIndex + 1;
    if (nextBlockIndex >= blocks.length) nextBlockIndex = 0;
    $('#' + blocks[curentBlockIndex]).slideUp(800, function(){
        $('#' + blocks[nextBlockIndex]).fadeIn(400);
    });

    curentBlockIndex = nextBlockIndex;
}, 4000);

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

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