简体   繁体   English

做/同时循环创建与setTimeout相结合的无限循环

[英]Do/While loop creating endless loop combined with setTimeout

I can't figure out why this wont work. 我不知道为什么这行不通。 I'm using inline opacity, so varX grabs fine. 我使用的是内联不透明度,因此varX可以正常使用。 But it seems to be creating and endless loop. 但这似乎是无休止的循环。 zIndex change works fine also on its own. zIndex更改本身也可以正常工作。 So I must be doing something wrong in do/while. 因此,我在做某件事时一定做错了。

Goal is to just change z-index once opacity has reached 0. 目标是仅在不透明度达到0时更改z-index。

HTML HTML

<div class="ribbon_services_body" id="ribbon_services_body_id" style=" opacity:1;">

JAVASCRIPT JAVASCRIPT

fade();
function fade() {

    setTimeout(function() {
        $("#ribbon_services_body_id").delay(0).animate({"opacity": "0"}, 800);

    },1000); 

    //var x = $('#ribbon_services_body_id').css('opacity');

    do {
        if($('#ribbon_services_body_id').css('opacity') == 0) {
            document.getElementById('ribbon_services_body_id').style.zIndex = "1";
        }
    }
    while ($('#ribbon_services_body_id').css('opacity') > 0.1);
}

CSS CSS

.ribbon_services_body {
    z-index:3;
    display:block;
    position: absolute;
    width: 100%;
    height: 10vh;
}

You're really doing this in a bad way. 您这样做的方式确实很糟糕。 Even if it worked (by moving the setting of "x" to the current opacity inside the loop) you're still burning CPU time in that loop. 即使它起作用(通过将“ x”的设置移动到循环内的当前不透明度),您仍然在该循环中消耗CPU时间。 Javascript isn't made to work this way because it'll block all other activity for the .8s while the opacity goes down. Javascript不能这样工作,因为它会在不透明度下降时阻止.8s的所有其他活动。 Do this instead: 改为这样做:

$("#ribbon_services_body_id").delay(1000).animate({
   opacity: 0
}, {
   complete: function() {
       document.getElementById('ribbon_services_body_id').style.zIndex = "1";
   },
   duration: 800
});

the problem with the endless loop is not going to be solved even if you put the x calculation inside the while loop. 即使将x计算放在while循环中,也无法解决无限循环的问题。

JS is single threaded application meaning that the setTimeout or animate is never going to be executed because you are trapped in the endless loop before exiting the function. JS是单线程应用程序,这意味着setTimeout或animate永远不会执行,因为在退出该函数之前您陷入了无限循环。 You will probably need this function to be calling itself. 您可能需要此函数来调用自身。

To get rid of this endless loop you have to also setTimeout the call to check, dont use a while, use a function that you can call to check the value every certain amount of miliseconds. 要摆脱这种无休止的循环,您还必须setTimeout调用进行检查,不要使用一段时间,而要使用可以调用的函数,每隔一定的毫秒数检查一次值。 this way you will be allowing the first settimeout and animate to work. 这样,您将允许第一个settimeout和动画生效。

Basically, you have to avoid blocking the thread. 基本上,您必须避免阻塞线程。 if a function is containing an endless loop, it doesn't matter if you have another function or a settimeout the whole js execution is trapped there. 如果一个函数包含一个无限循环,那么是否有另一个函数或settimeout无关紧要,整个js执行都被困在那里。 freezing the browser or any js runner. 冻结浏览器或任何js运行程序。

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

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