-1

Possible Duplicate:
Is there a sleep function in JavaScript?

How can i add a delay to this to make it close and redirect after so many seconds please?

<script type="text/javascript">
  $(function() { parent.$.fancybox.close(); });
  window.parent.location.href = '/shop/basket';
</script>
Community
  • 1
  • 1

3 Answers3

3
window.closeAndRedirect = function() {
    parent.$.fancybox.close();
    window.parent.location.href = '/shop/basket';
}

setTimeout(closeAndRedirect, 5000); // 5 seconds

@jAndy pointed out: why would you want to close something if you are redirecting anyway?, which is a valid point.

maybe you meant close, THEN redirect 5 seconds after closing? If so:

parent.$.fancybox.close(function() {
    setTimeout(function() {
       window.parent.location.href = '/shop/basket';
    });
});

assuming that close has a callback. If not, maybe:

parent.$.fancybox.close();
setTimeout(function() {
    window.parent.location.href = '/shop/basket';
});
georgedyer
  • 2,737
  • 1
  • 21
  • 25
1

Use setTimeout and call your function from the callback.

Something like:

$(function() { 
    setTimeout(function() {
        parent.$.fancybox.close(); 
        window.parent.location.href = '/shop/basket';
    }, 5000);
});

to wait 5 seconds, start from when the DOM is ready.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

Actually, you don't need to do that from within the opened (child) page but directly on the parent page.... just use setTimeout inside a callback in your custom fancybox script to program the close and redirection tasks like :

$(".fancybox").fancybox({
    afterLoad: function () {
        setTimeout(function () {
            $.fancybox.close();
            window.location.href = "/shop/basket";
        }, 5000); // 5000 = 5 secs
    }
});

See JSFIDDLE

JFK
  • 40,963
  • 31
  • 133
  • 306