Skip to content

Instantly share code, notes, and snippets.

@edwjusti
Created April 27, 2017 17:28
Show Gist options
  • Save edwjusti/e23f73996cb2c2a372642b83d205ad31 to your computer and use it in GitHub Desktop.
Save edwjusti/e23f73996cb2c2a372642b83d205ad31 to your computer and use it in GitHub Desktop.
setTimeout for php
<?php
class TimeoutClass extends Thread {
public function __construct(callable $cb, int $time, $args){
$this->callback = $cb;
$this->args = $args;
$this->time = $time * 1000;
}
public function run(){
usleep($this->time);
($this->callback)(...$this->args);
}
}
function setTimeout($cb, $time, ...$args){
static $count = 0;
$id = "timeout$count";
$GLOBALS[$id] = new TimeoutClass($cb, $time, $args);
$GLOBALS[$id]->start();
$count++;
}
//Basic usage
setTimeout(function(){
echo "Timeout has ended\n";
}, 2000);
//Passing arguments
setTimeout(function($x, $y){
echo "Passed $x and $y\n";
}, 2000, 25, 34);
@LukasCCB
Copy link

LukasCCB commented Aug 1, 2022

->start()

WTF?? where it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment