Is there any way to call a function 10 seconds after the page load in PHP. (Not using HTML.)
I wanted to do the same thing, to be notified at each hit my resume got. With flush() after all data sent then the DB and mail operations: the page on the client is fully rendered but the downloading progress bar is still present until the script is fully terminated.
I wanted to keep the whole stuff server-side (to allow the generated HTML file to be cleanly usable offline without giving errors) so JS wasn't an option.
I eventually ended simply appending a line with parameters to a text file, add a cron job every minute that compares this file size with the latest sent version and this bash script handles all the lenghty functions while the 9k page still loads and renders in a fraction of a second.
Unfortunately this method still has a up to 1 minute delay but still simple:
#!/bin/sh
FLOG=/home/web/traceur/cvaccess.txt
if [ -e $FLOG ]; then
if [ ! -e $FLOG.sent ]; then touch $FLOG.sent; fi;
SENT_LINES=$(wc -l $FLOG.sent | cut -d " " -f 1)
# No disk write if no new-data
if [ $(wc -l $FLOG | cut -d " " -f 1) -gt $SENT_LINES ]; then
cp -f $FLOG $FLOG.intr
NEW_LINES=$(wc -l $FLOG.intr | cut -d " " -f 1)
TO_SEND=$(( $NEW_LINES - $SENT_LINES ))
tail -n $TO_SEND $FLOG.intr > $FLOG.diff
mailx -s "Nouvelle consultation du CV" -r "HAL " jmarodon@jmd-tech.com < $FLOG.diff
rm $FLOG.diff
mv -f $FLOG.intr $FLOG.sent
fi
fi
And the page is at: http://www.jmd-tech.com/cv-julien-marodon.html, the PHP code is nothing more than those 3 lines at the end of the previously plain HTML file:
If i wanted to make a near-instant (<1s) or a 10 second delay version, i think the way to go would be using a daemon instead of a cron job and some kind of inter-process communication, probably a listening socket which the PHP script would fsockopen() for sending data and closing (fast), then the daemon proceeds by himself with lenghty operations.