简体   繁体   English

setTimeout似乎没有调用该函数

[英]setTimeout doesn't seem to be calling the function

The code worked great until I added the setTimeout. 在添加setTimeout之前,该代码非常有效。 Now, no matter how I attempt to call the functions in setTimeout ( setTimeout(function(){fadeOut()},1000); setTimeout("fadeOut()",1000); etc ) it doesn't seem to get to the function at all. 现在,无论我如何尝试在setTimeout中调用函数( setTimeout(function(){fadeOut()},1000); setTimeout("fadeOut()",1000);等),它似乎都无法到达发挥作用。

I'm a Javascript newbie so any and all help is appreciated =] 我是Javascript新手,因此感谢您提供所有帮助=]

javascript code: JavaScript代码:

var slideArray = ["slide1","slide2","slide3","slide4","slide5","slide6"];
var currentSlide = null;
var current = null;
var done = false;

function fade(newSlide)
{
    if(currentSlide === null)
    {
        currentSlide = slideArray[0];
        document.getElementById(currentSlide).style.opacity = 1.0;

        for(var i=1;i<slideArray.length;i++)
            document.getElementById(slideArray[i]).style.opacity = 0.0;
    }

    current = document.getElementById(currentSlide);
    done = false;
    do
    {
        window.setTimeout(fadeOut,1000);
    } while(done == false);

    currentSlide = newSlide;
    current = document.getElementById(currentSlide);
    done = false;

    do
    {
        window.setTimeout(fadeIn,1000);
    } while(done == false);
}

function fadeOut()
{
    if(parseFloat(current.style.opacity)-0.1>.0000001)
    {
    current.style.opacity = parseFloat(current.style.opacity) -0.1;
        done = false;
    }
    else
    {
        current.style.opacity = 0.0;
        done = true;
    }
}

function fadeIn()
{
    if(0.9-parseFloat(current.style.opacity)>.0000001)
    {
        current.style.opacity = parseFloat(current.style.opacity)+0.1;
        done = false;
    }
    else
    {
        current.style.opacity = 1.0;
        done = true;
    }
}

You can't use this structure: 您不能使用以下结构:

do
{
    window.setTimeout(fadeIn,1000);
} while(done == false);

Because the code in the setTimeout() runs sometime LATER, your value of done will NEVER be changed and this loop will run forever. 因为setTimeout()的代码稍后会运行,所以您的done值将永远不会更改,并且此循环将永远运行。 And, as long as it runs, the setTimeout() never gets to fire either (because javascript is single threaded). 而且,只要它运行, setTimeout()也不会触发(因为javascript是单线程的)。

Instead, what you should do is launch the next setTimeout(fadeIn, 1000) from the fadeIn() function if you aren't done. 相反,您应该做的是如果未完成, fadeIn()函数启动下一个setTimeout(fadeIn, 1000)

function fadeOut()
{
    if(parseFloat(current.style.opacity)-0.1>.0000001)
    {
        current.style.opacity = parseFloat(current.style.opacity) -0.1;
        setTimeout(fadeOut, 1000);
    }

    else
    {
        current.style.opacity = 0.0;
    }
}

Remember that javascript is single-threaded, so your setTimeout'et functions will not be called until it is finished running the current script. 请记住,javascript是单线程的,因此在完成当前脚本的运行之前,不会调用setTimeout'et函数。 Which will never happen since you're in a loop that's never gonna end (untill you're out of memory from all those setTimeout's). 这将永远不会发生,因为您处在一个永无止境的循环中(直到所有这些setTimeout的内存不足为止)。 Just call setTimeout once and let the function return. 只需调用一次setTimeout,然后让函数返回即可。 And forget the idea of waiting for it to have happened. 忘记了等待它发生的想法。

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

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