1. Go to this page and download the library: Download sue/event-loop library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Sue\EventLoop\setTimeout;
use Sue\EventLoop\loop;
use Sue\EventLoop\cancelTimer;
//延迟5秒执行
setTimeout(5, function () {
echo "hello world from 5 seconds ago";
});
loop()->run();
//提前终止执行
$timer = setTimeout(5, function () {
echo "hello world from 5 seconds ago";
});
cancelTimer($timer);
loop()->run();
use React\EventLoop\TimerInterface;
use function Sue\EventLoop\setInterval;
use function Sue\EventLoop\cancelTimer;
//每60秒执行一次
setInterval(60, function () {
echo "one minute has been passed\n";
});
// 中止运行
$timer = setInterval(1, function (string $name, int $age, TimerInterface $timer) {
if ($some_condition) {
cancelTimer($timer);
}
}, 'foo', 18);
loop()->run();
use React\EventLoop\TimerInterface;
use function Sue\EventLoop\setInterval;
use function Sue\EventLoop\cancelTimer;
$timer = setInterval(1, function (TimerInterface $timer) {
echo "working...\n";
});
if ($some_condition) {
cancelTimer($timer);
}
use RuntimeException;
use Sue\EventLoop\Exceptions\PromiseCancelledException;
use function Sue\EventLoop\nextTick;
$promise = nextTick(function () {
return "hello world";
});
$promise->then(function (string $content) {
//handler
});
//异常处理
$promise = nextTick(function () {
throw new RuntimeException('boom');
});
$promise->then(null, function (RuntimeException $e) {
echo "error: " . $e;
});
//中止执行
$promise = nextTick(function () {
return "hello world";
});
if ($some_condition) {
$promise->cancel();
}
$promise->otherwise(function (PromiseCancelledException $exception) {
//exception
});
use Sue\EventLoop\loop;
use Sue\EventLoop\throttle;
use Sue\EventLoop\setInterval;
$callback = function () {
echo "hello world\n";
};
setInterval(1, function () use ($callback) {
static $count = 10;
while ($count--) {
echo "tick\n";
throttle(3, $callback);
}
});
loop()->run();
/** expect output
tick
tick
tick
hello world
tick
tick
tick
hello world
tick
tick
tick
hello world
tick
**/
//如果想提前手动中止
$promise = throttle(3, $callback);
if ($some_condition) {
$promise->cancel();
}
$promise->otherwise(function (PromiseCancelledException $exception) {
//exception
});
use Sue\EventLoop\loop;
use Sue\EventLoop\debounce;
use Sue\EventLoop\setInterval;
$callback = function () {
echo "hello world\n";
};
debounce(1, function () use ($callback) {
static $count = 10;
while ($count--) {
echo "tick\n";
throttle(3, $callback);
}
});
loop()->run();
/** expect output
tick
tick
tick
tick
tick
tick
tick
tick
tick
tick
hello world
**/
//如果想提前中止
$promise = debounce(1, $callback);
if ($some_condition) {
$promise->cancel();
}
$promise->otherwise(function (PromiseCancelledException $exception) {
//exception
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.