dputlf5431 2012-06-01 21:40
浏览 123
已采纳

如何延迟代码执行?

How can I delay the execution of a block of code in php?

    if ($qry->execute($leads)) {
    echo'<div class="note success">' . 
            $_POST["first_name"] . ', Your entry was successful!
        </div>';

        // Set up email params

This code displays a "Successful" message then emails the users. After displaying the the message, I want to redirect the user.

Is this possible in PHP? Do I need to use JavaScript?

Thanks!

  • 写回答

3条回答 默认 最新

  • drnf09037160 2012-06-01 21:43
    关注

    If you want to delay the execution of PHP code, you can use the sleep function. However, it appears that you want to delay a redirect for the client. You can do this with JavaScript, <meta> refresh and the Refresh header.

    Using the Refresh header:

    header('Refresh: «timeout in seconds»; URL=http://example.com/myurl');
    

    Using <meta> refresh:

        <meta http-equiv="refresh" content="«timeout in seconds»; URL=http://example.com/myurl">
    

    Using JavaScript:

    setTimeout(function(){
     location.replace("/myurl");
    },«timeout in milliseconds»);
    

    The best is to use a combination of these three, and also show a message for users that have disabled these redirection technologies with a link that allows them to click through manually.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?