在php中设置后台计时器

I like to do a "background timer" but I don't know how. The main things: I have if, and if I go in I like to set the time, and start a timer ($ido = $split[0]*60*60 + $split[1]*60 + $split[2]; sleep($ido);), and after this timer end, I like to run a function ($jatek->epuletkesz();) but, after this if, I have another things, and if i start sleep the page loading is stop because wait for the sleep end. But I like, the sleep go in "background" and don't stop the page things, only the one function run after sleep end. (Sorry for bad English, I hope u can understand my problem.)

Here is the code:

if(empty($üzi)) {
    echo "<script type='text/javascript'>alert('Sikeres fejlesztés!');</script>";

    $nyers = $jatek->kovnyers($epulet);

    $split = explode(":", $nyers['epetesiido']);
    $ido = $split[0]*60*60 + $split[1]*60 + $split[2];
    sleep($ido);
    $jatek->epuletkesz();
    //header( 'Location: ../views/jatek.php' );
}

Well one possible solution is that you generate some classes in you css with different background. Like this :

.background1 {
    background: url('../img/img1.jpg');
}

.background2 {
    background: url('../img/img1.jpg');
}

In javascript you need a function to change the background like :

/**
 * elem jquery elemnt to update
 * background class to add
 */
var updateBackground = function(background, elem) {
    var bck_list = ['background1', 'background2'];
    elem.addClass(active);

    jQuery.each(bck_list, function( index, value ) {
        if (value !== active) {
            elem.removeClass(value);
        } else {
            var next_index = index + 1;
        }
    });

    // check if the next background exists
    if (bck_list[next_index] === undefined) {
         next_index = 0;
    }

    setTimeout(function(){ updateBackground(bck_list[next_index], elem); }, 30000);
}

At the end a timer :

setTimeout(function(){ updateBackground("background1", $('#elem_to_update')); }, 30000);

As PHP is not support threading, you need to emulate it somehow, in this case, just use the CURL and break the code logic into 2 part: the worker & the presenter

the presenter :

if( empty($üzi) ) {
        echo "<script type='text/javascript'>alert('Sikeres fejlesztés!');</script>";

        // the magic from here:
        $request = "URL_OF_YOUR_WORKER";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $request);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        curl_exec($ch);
        curl_close($ch);
    }

the worker:

        // some preparation
        set_time_limit(0);
        ignore_user_abort(TRUE); // this help to not terminate your worker

        // Your logic continue here...
        $nyers = $jatek->kovnyers($epulet);

        $split = explode(":", $nyers['epetesiido']);
        $ido = $split[0]*60*60 + $split[1]*60 + $split[2];
        sleep($ido);
        $jatek->epuletkesz();

As you can see, the presenter use a CURL to call the worker do something for him and because the CURL's timeout is almost immediately, your presenter just request and run away to response to user, let the worker run in background.

And on the worker, we put the ignore_user_abort statement to prevent your worker stop working even if the caller run away. After that, do what ever you want

Hope it can help