Is there a function similar to setTimeout() (JavaScript) for PHP?

Is there a function similar to setTimeout() (JavaScript) for PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why PHP Doesn't Have setTimeout() and How to Handle Timeouts Correctly

The immediate answer is no. There is no built-in function in PHP called setTimeout(). While JavaScript engines execute code asynchronously within a single thread, allowing setTimeout() to schedule a callback to run after a specific delay without blocking the main thread, PHP operates on a fundamentally different model. PHP scripts are typically stateless and synchronous by default. When you invoke a function in PHP, it executes immediately and returns control to the caller. The language does not maintain an internal event loop that would allow you to register a callback for execution several seconds later while the script continues processing other tasks.

This architectural difference stems from how web servers handle requests. In a standard shared hosting environment or a typical CLI execution, the PHP interpreter runs a script to completion and then terminates. There is no mechanism to keep the process alive and waiting for a timer to expire unless you explicitly instruct it to do so. Attempting to call sleep() followed by a callback function will not work as expected because the script ends before the callback can be invoked.

To achieve behavior similar to setTimeout(), you must architect your solution around persistent processes or external systems. The most common pattern for executing delayed tasks in PHP is to use a message queue system like Laravel Queues, which handles this complexity behind the scenes. You push a job into the queue with a delay attribute, and the worker process picks it up at the appropriate time.

Here is how you might implement a delayed execution using Laravel's built-in capabilities:

// Push a job to the queue with a 30-second delay
dispatch(new SomeJob($userId))->delay(now()->addSeconds(30));

In this scenario, the SomeJob class extends Illuminate\Queue\QueueableCommand. The application accepts the request, dispatches the command, and immediately returns control to the user. The actual execution happens asynchronously by a separate queue worker process running on your server. This approach decouples the immediate response from the background task, effectively simulating the non-blocking nature of setTimeout().

If you are working outside the Laravel ecosystem or need a lightweight solution without setting up a full queue infrastructure, you can utilize cron jobs. You would store a timestamp in your database representing when the task should run and configure a system cron job to execute a script that processes all due tasks every minute. This requires writing a scheduler script that scans for records where scheduled_time <= now() and executes the associated logic.

# Example cron entry to run a PHP script every minute
* * * * * php /path/to/process-due-tasks.php >> /var/log/php-tasks.log 2>&1

While this mimics the outcome of a delayed function, it lacks the precision and dynamic scheduling of setTimeout(). It is also less efficient for high-frequency or individual user-specific delays compared to queue-based systems.

Another consideration is using daemon processes with libraries like Supervisor. You can create a long-running PHP process that enters an event loop or polling cycle, checking for timers and executing callbacks accordingly. This approach brings the architecture closer to JavaScript's runtime but requires managing process lifecycles, logging, and restart policies manually.

The primary trade-off here is complexity versus control. JavaScript's setTimeout() is native because the browser environment is designed for single-threaded, event-driven execution. PHP's synchronous, request-limited nature makes a direct equivalent impossible without introducing external infrastructure. If you need this functionality, adopting a queue system or migrating to a framework that supports it, such as Laravel, provides the most robust and scalable solution.

For more guidance on implementing background jobs and optimizing queue configurations within the Laravel ecosystem, you can refer to resources like https://laravelcompany.com.

Our network