10

Is there a PHP equivalent to setting timeouts in JavaScript?

In JavaScript you can execute code after certain time has elapsed using the set time out function.

Would it be possible to do this in PHP?

kamikaze_pilot
  • 14,304
  • 35
  • 111
  • 171
  • 3
    Why? The browser wants to see the result of the php as soon as possible. Can you give more detail? – mplungjan Jun 26 '11 at 19:59
  • 1
    See sleep() (http://php.net/sleep), but as @mplungjan says: why!? – Matt Jun 26 '11 at 20:00
  • The script usually has only 30 seconds or so to execute before the server kills it anyway. – Richard Jun 26 '11 at 20:03
  • I've actually used sleep() before to simulate the processing time of the code before I've written the code. This is helpful for testing GUIs/frontends. – Richard Jun 26 '11 at 20:04
  • @mplungjan PHP isn't only used to process dynamic web pages. You can use to to run cron jobs, web scraping, even crunch some data. I use it to auto generate specially formatted classes in C# based on xml documents (just an example). – PiZzL3 Jun 26 '11 at 20:04
  • @Richard that depends on php.ini settings. That is also an adjustable limit. You can run a single php script forever if you want. – PiZzL3 Jun 26 '11 at 20:05
  • 1
    @PiZzL3 php and javascript sounds to me like browser based stuff and not a crontab unless it is mis-tagged. Hence the Why - if not browser based you are of course correct – mplungjan Jun 26 '11 at 20:09
  • Repliers should first ask this guy why does he want to do settimeout.. I feel they provide the answer for the wrong question :/ – Karoly Horvath Jun 26 '11 at 21:28

5 Answers5

6

PHP is single-threaded, and in general PHP is focused on the HTTP request cycle, so this would be tricky to allow a timeout to run code, potentially after the request is done.

I can suggest you look into Gearman as a solution to delegate work to other PHP processes.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
5

You can use the sleep() function:

int sleep ( int $seconds )

// Delays the program execution for the given number of seconds. 

Example:

public function sleep(){
   sleep(1);
   return 'slept for 1 second';
}
Asaf Hananel
  • 7,092
  • 4
  • 24
  • 24
3

This is ugly, but basically works:

<?php
declare(ticks=1);

function setInterval($callback, $ms, $max = 0)
{
  $last = microtime(true);
  $seconds = $ms / 1000;

  register_tick_function(function() use (&$last, $callback, $seconds, $max)
  {
    static $busy = false;
    static $n = 0;

    if ($busy) return;

    $busy = true;

    $now = microtime(true);
    while ($now - $last > $seconds)
    {
      if ($max && $n == $max) break;
      ++$n;

      $last += $seconds;
      $callback();
    }

    $busy = false;
  });
}

function setTimeout($callback, $ms)
{
  setInterval($callback, $ms, 1);
}

// user code:

setInterval(function() {
  echo microtime(true), "\n";
}, 100); // every 10th of a second

while (true) usleep(1);

The interval callback function will only be called after a tickable PHP statement. So if you try to call a function 10 times per second, but you call sleep(10), you'll get 100 executions of your tick function in a batch after the sleep has finished.

Note that there is an additional parameter to setInterval that limits the number of times it is called. setTimeout just calls setInterval with a limit of one.

It would be better if unregister_tick_function was called after it expired, but I'm not sure if that would even be possible unless there was a master tick function that monitored and unregistered them.

I didn't attempt to implement anything like that because this is not how PHP is designed to be used. It's likely that there's a much better way to do whatever it is you want to do.

Matthew
  • 47,584
  • 11
  • 86
  • 98
0

Without knowing a use-case for your question it's hard to answer it:

  • If you want to send additional data to the client a bit later you can do a JS timeout on the client side with a handler that will make a new HTTP request to PHP.
  • If you want to schedule some task for a later time you can store that in a database and poll the DB in regular intervalls. It's not the best peforming solution but relatively easy to implement.
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

if ($currenturl != $urlto) exit( wp_redirect( $urlto ) );

You can replace above two line with below code inside your function

if ($currenturl != $urlto)
header( "refresh:10;url=$urlto" );

Support_R
  • 44
  • 5