Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7033193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:01:59+00:00 2026-05-28T01:01:59+00:00

I tried doing a JS fade effect with the setTimeout() function and it appeared

  • 0

I tried doing a JS fade effect with the setTimeout() function and it appeared to work to some extent, but I can’t seem to figure out what’s wrong with the code below:

<html>
<head>
<title></title>
<script type="text/javascript">
function FadeEffect(n)
{
    var i=1;
    fade = document.getElementById("box");
    if (n===1)
    {
        fade.style.opacity=i/10;
        i++;
        setTimeout("FadeEffect(1)",50);
        if (fade.style.opacity=1)
        {
         var i=1;
        }
    }
    else if (n===0)
    {
        fade.style.opacity=1-i/10;
        i++;
        setTimeout("FadeEffect(0)",50);
        if (fade.style.opacity=0)
        {
        var i=1;
        }
    }
}


</script>
<style type="text/css">
#box{
width: 200px;
height: 50px;
border: 1px solid black;
background-color: #ccc;
opacity: 0;
}
</style>
</head>
<body>
<div onMouseOver="FadeEffect(1)" onMouseOut="FadeEffect(0)" id="box">Menu</div>
</body>
</html>

Edit: updated the code with setTimeout() functions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:02:00+00:00Added an answer on May 28, 2026 at 1:02 am

    There are two problems with the function that I can see.

    First, your if statements both do an assignment rather than a comparison. You are saying if (n=0) (one =, assignment) when you should be saying if (n===0) (three ===, comparison, or you can use two == for a type-converting comparison).

    Second, using a for loop to repeatedly change the style isn’t going to fade because the browser doesn’t update the display at the same time as your code is executing – essentially it uses the same thread for display and for JavaScript. So the page will be updated after the function exits. You need to give the browser a chance to update after each iteration by using setTimeout() – something like:

    function fadeEffect(element,startValue,target,delay){
       element.style.opacity = startValue;
       if (startValue < target)
          startValue = Math.min(startValue + 0.1, target);
       else
          startValue = Math.max(startValue - 0.1, target);
    
       if (startValue != target)
          setTimeout(function(){fadeEffect(element,startValue,target,delay);}, delay);
    }
    
    <div onMouseOver="fadeEffect(this, 1, 0, 100);"
         onMouseOut="fadeEffect(this, 0, 1, 100);" id="box">Menu</div>
    

    Demo: http://jsfiddle.net/hLQ6y/2/

    EDIT: Note that this code doesn’t cope all that brilliantly if you move the mouse in and out too quickly, i.e., if you trigger the fade in before the fade out has finished. (You can see what I mean in my jsfiddle.) You can solve this by saving the return from .setTimeout() and calling .clearTimeout() if required. Given that I’ve already covered the essence of the question I’ll leave the fine-tuning as an exercise for the reader…

    UPDATE: Your updated code has introduced new if statements with the same assignment-instead-of-comparison problem. Also it is calling setTimeout() forever – you should do that conditionally as in the answers Pointy and I gave. Also you seem to be relying on the variable i which, as a local variable, will not retain its value between calls – you could make it a global, but better to manage it as a parameter like I did or as a local variable in an outer function like Pointy did.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried doing it by using Intent,but,i seem to get the following error: 11-15
I tried doing something like: cmbMyBox.ListFillRange = E2 But the combobox does not seem
I tried doing this: [toolbar setTint:[UIColor colorWithPatternImage:[UIImage imageNamed:@thingFromMyBundle.png]]]; but it just ended up black.
Just tried to execute a small Lua script, but unfortunately I'm doing something wrong.
I tried doing this but then it is not working <?php if ($_SERVER['SERVER_NAME']=='http://www.testground.idghosting.com/idi' &&
I tried doing this but it failed. SELECT table2.ID, table1.* FROM table2 LEFT JOIN
I havent found info on this subject yet, but maybe someone have tried doing
I tried doing this: - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { return NO; } but it still
I tried doing selecting multiple rows using jquery but this code look like cranky.
How can I compare image sizes? I tried doing something like this: if (image1.image.size

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.