-1

给定 PHP 中的一个输入和一个按钮,我需要在输入中输入一个时间量,然后按下按钮,它应该在该时间量之后执行一个 PHP 函数。

我该怎么做呢?

编辑:

我不需要客户端解决方案。这是服务器端的问题。

当用户离开页面时,JavaScript 的超时终止。用户离开页面后,我需要该超时仍然存在。

我没有尝试任何东西,我不知道如何做到这一点。

4

2 回答 2

1

由于几乎所有时间都在处理数据库中的数据,因此在 mysql 中创建事件可能是一种选择。此外,如果你设法全部用 mysql 代码编写,它会比从 php 到 mysql 来回来回更快。

于 2012-10-05T05:23:37.637 回答
1

我最近也有类似的需求。不确定这是否适合您。让我知道它是否有效。

if (isset($_GET['async'])) {

    for( $i = 0 ; $i < 10 ; $i++ )
    {
        append_log(date('l jS \of F Y h:i:s A') . ': background process.<br />');
        sleep(1);
    }

    exit;
}

header( 'Content-type: text/html; charset=utf-8' );
pseudo_async(array('async' => true));   // this runs this php script in the backbround

echo 'Begin ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
    output_buffer('appended to the log <br />');
    append_log(date('l jS \of F Y h:i:s A') . ': main process.<br />');
    sleep(1);
}
echo 'End ...<br />';


function pseudo_async($query) {

    $timeout = array('http' => array('timeout' => 0.01));
    $context = stream_context_create($timeout);

    @file_get_contents(selfurl() . '?' . http_build_query($query), false, $context);
}

function append_log($msg) {
    $file = __DIR__ . '/log.html';
    file_put_contents($file, $msg, FILE_APPEND);
}

function selfurl() {
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80")
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    else 
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    return $pageURL;
}

function output_buffer($str) {
    echo $str;
    flush();
    ob_flush(); 
}
于 2012-10-05T06:22:09.617 回答