views:

41

answers:

3

Hi, I've readen a book that uses Java's Timer class... something like:

final Timer timer = new Timer();
timer.schedule(new TimerTask() {
    public void run() {
        /* Does something */
        timer.cancel();
    }
}, 5000);

Is there a way to implement that in PHP? I'm rewrinting the book examples in PHP. Thank you.

A: 

I don't believe that PHP has any timer functions because it actually runs before the content is pushed to the browser. PHP's job is done once it has communicated with the server and back to the client computer.

Javascript most certainly has timer features though, in the form of setTimeout() and setInterval().

Allen Gingrich
A: 

PHP does contain a sleep() function, a time_sleep_until() function and a set_time_limit() function, but there is no direct analog of Java's Timer class.

Related question: Does PHP have threading?

Lord Torgamus
A: 

You could accomplish a type of timed scheduler using a combination of PHP's declare construct ( http://us2.php.net/manual/en/control-structures.declare.php ), and the register_tick_function ( http://us2.php.net/manual/en/function.register-tick-function.php ).

I've never tried doing anything like that, and I'm not sure what the performance would be like.

mellowsoon