@
szopen 貌似没有复杂啊
<?php
//enable ticks
declare (ticks = 1);
//setTimeout event list
$timeoutQueue = new SplObjectStorage;
register_tick_function(function() {
global $timeoutQueue;
foreach ($timeoutQueue as $v) {
$callBack = $v();
if (is_callable($callBack)) {
$callBack();
$timeoutQueue->detach($v);
}
}
});
register_shutdown_function(function() {
global $timeoutQueue;
while($timeoutQueue->count()) {
foreach ($timeoutQueue as $v) {
$callBack = $v();
if (is_callable($callBack)) {
$callBack();
$timeoutQueue->detach($v);
}
}
usleep(1000);
}
});
function setTimeOut($callback, $time)
{
global $timeoutQueue;
$end = microtime(true) * 1000 + $time;
$f = function() use ($callback,$end) {
if (microtime(true)*1000 >= $end) {
return $callback;
} else {
return false;
}
};
$timeoutQueue->attach($f);
return $f;
}
function clearTimeout(Closure $f) {
global $timeoutQueue;
$timeoutQueue->detach($f);
}
setTimeOut(function() {
echo 'aaaa';
},3000);
setTimeOut(function() {
echo 'bbbb';
},6000);