0

I would like to run part of php code asynchronously. I want to run the insertnumber() function after 60seconds but without delaying the whole code.

 echo json_encode($returnArray);
 if ($timeout=="1")
{
  sleep(60);
  $connection->insertnumber($finalnumber,"Loser");
} 

This method delay the whole code by 60 secs. I want that part of the code to run in the background without affecting the original code and then executing the insernumber() function after 60 secs.

Ahmed
  • 1,229
  • 2
  • 20
  • 45
  • 2
    Possible duplicate of [PHP how to have code continue without waiting for results of include](https://stackoverflow.com/questions/22925550/php-how-to-have-code-continue-without-waiting-for-results-of-include) – Michel May 25 '18 at 06:23

1 Answers1

0

PHP by default runs synchronously. Have a look at the following question which has a few different methods on how to run PHP asynchronously, using either PHP OOP Threads or some built it PHP Process methods:

How can one use multi threading in PHP applications

Additionally, if you are building something that will be running in the browser, have a look at using Javascript asynchronous functions to achieve what you desire:

What is a simple example of an asynchronous javascript function?

Gary Thomas
  • 2,291
  • 1
  • 9
  • 21