-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtimer.php
More file actions
executable file
·33 lines (28 loc) · 939 Bytes
/
timer.php
File metadata and controls
executable file
·33 lines (28 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* How to run this script:
* docker compose exec -t server bash -c "./timer/timer.php"
*
* Check the output and see how Timer works in Swoole.
*
* The example can be implemented using coroutines only (without the \Swoole\Timer class). Please check script
* "coroutine-style.php" for details.
*/
use Swoole\Timer;
use function Swoole\Coroutine\run;
run(function (): void {
$id = Timer::tick(100, function (): void {
echo 'Function call is triggered every 100 milliseconds by the timer.', PHP_EOL;
});
Timer::after(500, function () use ($id): void {
Timer::clear($id);
echo 'The timer is cleared at the 500th millisecond.', PHP_EOL;
});
Timer::after(1000, function () use ($id): void {
if (!Timer::exists($id)) {
echo 'The timer should not exist at the 1,000th millisecond.', PHP_EOL;
}
});
});