how to call a function in PHP after 10 seconds of the page load (Not using HTML)

后端 未结 12 886
终归单人心
终归单人心 2020-12-11 02:44

Is there any way to call a function 10 seconds after the page load in PHP. (Not using HTML.)

相关标签:
12条回答
  • 2020-12-11 03:24

    Not really, no. 10 seconds after your page loaded is (at least) 10 seconds after your PHP script finished, i.e. it is no longer running (apart from tricks that try keeping the connection open, which I don't think will work for a time as long as 10 seconds)!

    Therefore, you either need to schedule a cron job on the server side to fire in 10 seconds, or you need a callback from the website, using AJAX.

    0 讨论(0)
  • 2020-12-11 03:24

    There's no way you can do it with PHP, except maybe using some crontab / loop with sleep() and file_get_contents(). Or use javascript/ajax as previously mentioned.

    0 讨论(0)
  • 2020-12-11 03:25

    This seems weird idea but maybe it's what you are looking for if you want to do it in PHP without touching HTML/JS:

    <?php
    your_website_here();
    
    flush(); //this sends the output to the client. You may also need ob_flush();
    sleep(10); //wait 10 seconds
    
    your_func_here();
    ?>
    

    The above is preety OK in theory, but in practice it will result in VERY memory consuming app. So be warned.

    And please don't downvote me, this is only a theoretical dispute.

    0 讨论(0)
  • 2020-12-11 03:29

    Php is a servide scripting language, and can't detect if the page is loaded or not. so you have to use client side script javascript.

    0 讨论(0)
  • 2020-12-11 03:29

    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 <hal@jmd-tech.com>" 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:

    <?php
    // Enregistrement log
    $ligne=$_SERVER["REMOTE_ADDR"]."\t".$_SERVER["HTTP_USER_AGENT"]."\t".$_SERVER["HTTP_REFERER"]."\t".date("Y-m-d H:i:s");
    $fic=fopen("/home/web/traceur/cvaccess.txt","a");
    if ($fic) { fwrite($fic,$ligne."\n"); fclose($fic); }
    ?>
    

    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.

    0 讨论(0)
  • 2020-12-11 03:30

    The closest think I can think of is :

    Once your script has finish to execute, it saves an entry in a databases with the time. Then, a daemon (cron style) execute every second each instruction in the databases that is older than 10 seconds.

    0 讨论(0)
提交回复
热议问题