If the only goal is to execute a function some time after a web-site has been loaded, you can do that with a bit of trickery. The original script execution can't be delayed without holding up the client, but you can have that script set another script up to execute the function on a timer.
One cavity in this is that the following will only work on Linux hosts. I've got no idea if something equivalent exists for Windows, but I do know that
this particular method does not work on Window.
OK, as an example, you could do this inside your main PHP script:
-
$time = time();
-
shell_exec("php delayed.php {$time} >/dev/null&");
-
This will trigger the "delayed.php" script to execute in the background, without it pausing the original PHP script while it finishes. Whatever happens in the "delayed.php" script happens completely separately.
So even if the original script redirects the client and exits, the delayed script will continue executing until
it has finished it's job. You can use sleep functions there without any trouble:
-
<?php
-
if (@$argc > 1 && (int)$argv[1] > 0) {
-
sleep(2);
-
require "inc/functions.php";
-
doDelayedStuff((int)$argv[1]);
-
}
-
else {
-
error_log("Invalid time parameter passed to the delayed script.");
-
}
-
This will execute some function 2 seconds after it is called, passing it the time value passed to the script. (That whole time thing is just an example of how you could pass values between the original script and the delayed script.)