Hướng dẫn settimeout equivalent in php

This is ugly, but basically works:

 $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.

    Table of contents
  • Asynchronous PHP: Why?
  • Is there a set time out equivalent in php?
  • GearmanWorker::setTimeout

Asynchronous PHP: Why?

setTimeout(function() {
    console.log('After timeout');
}, 1);

console.log('Before timeout');
Before timeout
After timeout

Is there a set time out equivalent in php?

int sleep ( int $seconds )

// Delays the program execution for the given number of seconds. 
public function sleep(){
   sleep(1);
   return 'slept for 1 second';
}
 $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);

GearmanWorker::setTimeout

Starting
Waiting for job...
Timeout. Waiting for next job...
Timeout. Waiting for next job...
Timeout. Waiting for next job...

JavaScript setTimeout() – How to Set a Timer in JavaScript or Sleep for N Seconds

setTimeout(function(){
    console.log("Hello World");
}, 2000);

console.log("setTimeout() example...");
setTimeout(function, milliseconds, parameter1, parameter2, ...);
function greeting(){
  console.log("Hello World");
}

setTimeout(greeting);
function greeting(){
  console.log("Hello World");
}

setTimeout(greeting, 3000);
function greeting(name, role){
  console.log(`Hello, my name is ${name}`);
  console.log(`I'm a ${role}`);
}

setTimeout(greeting, 3000, "Nathan", "Software developer");
setTimeout(greeting("Nathan", "Software developer"), 3000);
clearTimeout(id);
const timeoutId = setTimeout(function(){
    console.log("Hello World");
}, 2000);

clearTimeout(timeoutId);
console.log(`Timeout ID ${timeoutId} has been cleared`);

Next Lesson PHP Tutorial

Questions : Is there a set time out equivalent in php

2022-09-24T12:39:41+00:00 2022-09-24T12:39:41+00:00

5427

Is there a PHP equivalent to setting anycodings_javascript timeouts in JavaScript?

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

Would it be possible to do this in PHP?

Total Answers 5

25

Answers 1 : of Is there a set time out equivalent in php

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

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

0

2022-09-24T12:39:41+00:00 2022-09-24T12:39:41+00:00Answer Link

mRahman

5

Answers 2 : of Is there a set time out equivalent in php

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';
}

0

2022-09-24T12:39:41+00:00 2022-09-24T12:39:41+00:00Answer Link

joy

6

Answers 3 : of Is there a set time out equivalent in php

This is ugly, but basically works:

 $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 anycodings_php be called after a tickable PHP anycodings_php statement. So if you try to call a anycodings_php function 10 times per second, but you anycodings_php call sleep(10), you'll get 100 anycodings_php executions of your tick function in a anycodings_php batch after the sleep has finished.

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

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

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

0

2022-09-24T12:39:41+00:00 2022-09-24T12:39:41+00:00Answer Link

jidam

6

Answers 4 : of Is there a set time out equivalent in php

Without knowing a use-case for your anycodings_php 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.

0

2022-09-24T12:39:41+00:00 2022-09-24T12:39:41+00:00Answer Link

miraj

6

Answers 5 : of Is there a set time out equivalent in php

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

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

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

0

2022-09-24T12:39:41+00:00 2022-09-24T12:39:41+00:00Answer Link

joy