Imri Persiado
Imri Persiado

Reputation: 1889

Using the php sleep() function

I'm trying to print something, wait 5 seconds and move to another page. I'm using the sleep function but for some reason nothing is printed and looks like it skips the print part.

It looks like this:

<?php
echo "Thank you!";
sleep(5);
?>
<script type="text/javascript"> window.location = '?a=home'; </script>

Upvotes: 3

Views: 1576

Answers (5)

skparwal
skparwal

Reputation: 1084

You can do it in javascript or using headers.

in js:

<script>
window.setTimeout('window.location = "?a=home"', 5000); //5000 = 5 secs

using headers:

header('Refresh: 5;url=page.php?a=home'); // 5 = 5 secs

related post: How to Redirect Page in PHP after a few seconds without meta http-equiv=REFRESH CONTENT=time

Upvotes: 1

Bouffe
Bouffe

Reputation: 849

You can use javascript to do that :

setTimeout('window.location.replace("?a=home")',5000);

Upvotes: 2

Erik Schierboom
Erik Schierboom

Reputation: 16636

Perhaps it is better to use the HTML meta refresh tag. It has the same functionality but does so from the client:

<meta http-equiv="refresh" content="5;URL='?a=home'">

You can find more information here: http://www.metatags.info/meta_http_equiv_refresh

Upvotes: 5

Ronnie Jespersen
Ronnie Jespersen

Reputation: 950

I'm pretty sure that using sleep just delays the program execution. You will only see the content when your PHP script is done and you are preventing it from doing so because of the sleep function.

Upvotes: 0

Vlad Bereschenko
Vlad Bereschenko

Reputation: 348

You need to remove php and use only javascript:

<script type="text/javascript"> setTimeout(
function() {
    window.location = '?a=home';
}, 5000);
</script>

Upvotes: 11

Related Questions