最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript recursive timeout call - Stack Overflow

programmeradmin0浏览0评论

This is my attempt at writing a dynamic onmouseout event that slowly changes the opacity when the mouse leaves the div. For some reason the recursion and timeout seem to be no working property and the change in opacity is done immediately.

The question: Is there any reasons that setTimeout() does not work with recursion? Is there a better way to approach this problem?

function hide(id)
{
    if (gOpacity > .4) 
    {
        gOpacity -= .1;
        document.getElementById(id).style.opacity = gOpacity;
        setTimeout(hide(id),1000)
    }
    else 
    {
        gOpacity = 1.0
    }
}

This is my attempt at writing a dynamic onmouseout event that slowly changes the opacity when the mouse leaves the div. For some reason the recursion and timeout seem to be no working property and the change in opacity is done immediately.

The question: Is there any reasons that setTimeout() does not work with recursion? Is there a better way to approach this problem?

function hide(id)
{
    if (gOpacity > .4) 
    {
        gOpacity -= .1;
        document.getElementById(id).style.opacity = gOpacity;
        setTimeout(hide(id),1000)
    }
    else 
    {
        gOpacity = 1.0
    }
}
Share Improve this question edited Jun 2, 2013 at 20:40 Jan Turoň 32.9k23 gold badges137 silver badges177 bronze badges asked Jun 2, 2013 at 20:36 IntriquedManIntriquedMan 2331 gold badge8 silver badges15 bronze badges 3
  • 2 possible duplicate of Why is the method executed immediately when I use setTimeout? – Quentin Commented Jun 2, 2013 at 20:40
  • The answers below show the fix, but to add to the discussion, setTimeout works just fine recursively. setTimeout takes a function as it's first parameter, but instead, you're passing in the result of a function call. hide(id) invokes the function immediately. remove the (), and you're fine. – Alan Commented Jun 2, 2013 at 20:46
  • This function is essentially a "fade" out over a long period of time. Is there a reason you're not using a CSS Transition instead? On modern browsers CSS animations are hardware accelerated. – Alan Commented Jun 2, 2013 at 20:48
Add a comment  | 

4 Answers 4

Reset to default 11

Change your setTimeout call to

setTimeout(function() { hide(id); } ,1000)

So it will execute after 1s, and not immediately

Have you tried this?

function hide(id)
{
    if (gOpacity > .4) 
    {
        gOpacity -= .1;
        document.getElementById(id).style.opacity = gOpacity;
        setTimeout(function() {
           hide(id);
        },1000)
    }
    else 
    {
        gOpacity = 1.0
    }

}

i thinck that when you type :

 setTimeout(hide(id),1000);

what you really mean is :

  setTimeout(hide.bind(this,id),1000);

becasue in the first case, the function will be call instantly when setTimeout is call - it is an argument of setTimeout- .
In the second case, this will be a bound function. So yes this and id are evaluated, but the function is not called until the 1000 ms elapsed.

(this is just a faster to run / faster to type way of creating a function ).

wrap all your code in recursive function by setTimeout

function hide(id)
{
    setTimeout(function() {
        if (gOpacity > .4) 
        {
            gOpacity -= .1;
            document.getElementById(id).style.opacity = gOpacity;   
            hide(id);
        }
        else 
        {
            gOpacity = 1.0
        }
    },1000)
}

发布评论

评论列表(0)

  1. 暂无评论