user2557352
user2557352

Reputation: 13

Delaying JavaScript code

On my webpage I have a piece of javascript that allows a div to fade in

<script type="text/javascript">
    $(document).ready(function () {

        $('#page_effect').fadeIn(10000);

    });
</script>

But, I would like the same div to fade out. I have the code for the fade out but I'm wondering how I would delay the javascript code from running, for about 140 seconds

<script type="text/javascript">
    $(document).ready(function () {

        $('#page_effect').fadeOut(140000);

    });
</script> 

Upvotes: 1

Views: 92

Answers (5)

Mark Walters
Mark Walters

Reputation: 12390

Try this

<script type="text/javascript">
    $(document).ready(function () {
        setTimeout(function() {
           $('#page_effect').fadeOut();
        }, 140000);
    });
</script> 

Here is a test fiddle with a smaller delay (so that you don't have to wait for the full 140 seconds of course)

More info on setTimeout here

Another option would be to use jQuery's .delay() method, referenced here

Upvotes: 3

Mahesh Thumar
Mahesh Thumar

Reputation: 4395

You can use simple setTimeout(); like this:

 $(document).ready(function () {

   setTimeout(function(){ $('#page_effect').fadeOut(140000);},140000);

});

This run fadeout after 140 sec if you want reverse then use callback of fadeOut like this

 $('#page_effect').fadeOut(140000,function()
   {alert("I will be called after 140 sec")}
 );

Upvotes: 0

Johan Nordberg
Johan Nordberg

Reputation: 3777

Assuming you are using jQuery you can use the .delay() method. Have a look at the documentation and you'll find an example too.

https://api.jquery.com/delay/

Upvotes: 0

kamesh
kamesh

Reputation: 2424

This may be helpful for you.. use set interval for you function set interval

Upvotes: 0

The Angry Saxon
The Angry Saxon

Reputation: 792

Try reading about setTimeout http://www.w3schools.com/jsref/met_win_settimeout.asp.

Upvotes: 0

Related Questions