I have a php file test.php
. I want to echo
or print
"Success" after 5 seconds, soon after the php file is called or loaded or opened by the browser. Btw, Sometimes I may want to execute / initialise some functions after a specific interval of time.
Can anybody tell / suggest me how can I make a time-oriented task using php, like printing a message after 5 seconds??
Thanks in advance..
You can interrupt the execution of your script using
sleep()
. If you want sub-second precision, you can useusleep()
:If you have to, you can use this: http://php.net/manual/en/function.register-tick-function.php but watch out for impact on performance.
In the called function you check if enough time has passed, and if so unregister the tick function and then run the appropriate code.
It is usually not a good idea to do this in PHP. The PHP script should run as quickly as possible. Delaying the PHP execution of the PHP script
The best alternative is JavaScript and its setTimeout():
(alternatively, instead of
alert()
, you could instruct JavaScript to show a dialog or similar.)if you do not want to depend on JavaScript, you could consider a META redirect taking the user to a page containing the "Done!" message.
You can use jQuery timer to delay execution of subsequent items in the queue
http://api.jquery.com/delay/
http://www.w3schools.com/js/js_timing.asp