2

In this case, how can I execute a timeout before the fadeOut occurs? (Right where the comment is).

HTML

<button id="button"> Button </button>

<h1 id="one"> Test</h1>

CSS

#one {display:none;}

Javascript

$(document).ready(function(){
 $("#button").click(function(){

  $("#one").fadeIn('slow', (function(){
/* SetTimoeout */
  $("#one").fadeOut('slow');

 }));
})
});

1 Answer 1

1

Try this :

$(document).ready(function(){

    $("#button").click(function(){

    $("#one").fadeIn('slow', function(){

        $("#one").delay(3000).fadeOut('slow');

       })
    })
})

Final code :

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        
        #one {
            display: none;
        }
        
    </style>
</head>
    <body>
        
        <button id="button"> Button </button>
        <h1 id="one"> Test</h1>
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            
    $(document).ready(function(){

        $("#button").click(function(){

        $("#one").fadeIn('slow', function(){

            $("#one").delay(3000).fadeOut('slow');

           })
        })
    })
        
        </script>
    </body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it worked both ways but that's not what I'm asking. I want to place a time out where I have my comment, so it fades in, waits the set amount of time, then it fades out.
Yes I fixed it with delay(), my bad. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.