0

I am trying to create a function that after some time spending on a specific page, without any action of user, it will redirect him into another page, but with a fade out effect. Everything works good, except that the current page doesn't fade out, just simply goes to the new page. here is my code:

setTimeout(function(){
$('body').fadeOut('slow');
window.location.href = "index.html";
}, 6000);
Bogdan Florin
  • 103
  • 2
  • 8
  • 1
    [`fadeOut`](http://api.jquery.com/fadeout/#fadeOut-duration-complete) takes an optional callback function: put the `href` setting in that. – Heretic Monkey Sep 17 '18 at 22:12

3 Answers3

2

Put the location change in a callback to be run after the fadeOut completes.

setTimeout(function(){
    $('body').fadeOut('slow', function() {
        window.location.href = "index.html";
    });
}, 6000);

This might make your setTimeout redundant. You probably just want:

$('body').fadeOut('slow', function() {
    window.location.href = "index.html";
});
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
2

The fadeOut method accepts another argument which is a callback function that's executed when the animation is complete, so, you can pass, alongside of the duration argument, a function that contains the redirection statement:

setTimeout(function() {
  $('body').fadeOut('slow', function() {
    window.location.href = "index.html";
  });
}, 6000);

Hope I pushed you further.

ThS
  • 4,597
  • 2
  • 15
  • 27
0

You should use fadeout() in document or html section:

$(document).fadeOut();

or

$("html").fadeOut();

I hope this helps.

Fabricio
  • 3,248
  • 2
  • 16
  • 22