Created
April 27, 2017 17:28
-
-
Save edwjusti/e23f73996cb2c2a372642b83d205ad31 to your computer and use it in GitHub Desktop.
setTimeout for php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
->start()
WTF?? where it