PHP Classes


Recommend this page to a friend!
  Classes of Ahmad Mustapha   React PHP Timer Loop   Download  
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: React PHP Timer Loop
Call functions after a given time like JavaScript
Author: By
Last change:
Date: 2 years ago
Size: 1,936 bytes


Class file image Download

<h1>REACTPHP-TIMER</h1> <hr>

What is this?

Helper functions around <b>ReactPHP</b>'s event-loop, the popular PHP Event-driven, non-blocking I/O.</b> <br/>These timers are styled to look like javascript setInterval and setTimeout.


Make sure that you have composer installed Composer.

If you don't have Composer run the below command

curl -sS | php

Now, let's install the Timers:

composer require ahmard/reactphp-timers 1.*

After installing, require Composer's autoloader in your code:

require 'vendor/autoload.php';


use React\EventLoop\Factory;

$loop = Factory::create();

  • setTimeout(float $interval, callable $callback): void
    setTimeout(1.2, function(){
    echo "Hello World\n";
  • setInterval(float $interval, callable $callback): void
    setInterval(1, function(){
    static $count = 1;
    echo "Count: {$count}\n";
  • clearTimeout(React\EventLoop\Timer\Timer $timer): void
    $timeout = setTimeout(1.2, function(){
    //The following code will not run
    echo "Hello Planet\n";
  • clearInterval(React\EventLoop\Timer\Timer $timer): void
    setInterval(1.2, function($timer){
    //The following code will only run once
    echo "Hello World\n";
  • clearTimer(React\EventLoop\Timer\Timer $timer): void
    <br/>This method will clear all timers(interval & timeout)
    $timeout = setTimeout(1.2, function(){
    echo "Hello World\n";

$interval = setInterval(0.7, function(){

echo "Hello Planet Earth.\n";

}); clearTimer($interval);

- getLoop(): React\EventLoop\StreamSelectLoop;

$loop = getLoop();

# Examples