Skip to content

Commit c7cc45d

Browse files
author
Greg Bowler
committed
Add tiny tick timer to example
1 parent 2180ce3 commit c7cc45d

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

example/02-countdown.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
* This example shows how a PeriodicTimer can be used to schedule ticks
44
* forever, combined with an IndividualTimer that will cause the loop to halt
55
* after a specified period.
6+
*
7+
* To articulate the use of asynchronous methods, a "tiny tick timer" is
8+
* introduced that runs very frequently, alongside the countdown. You can
9+
* imagine the concurrent timers being used to execute actual workloads.
610
*/
711

812
use Gt\Async\Loop;
@@ -13,6 +17,7 @@
1317

1418
$stopTimer = new IndividualTimer(5);
1519
$periodicTimer = new PeriodicTimer(1, true);
20+
$tinyTickTimer = new PeriodicTimer(0.1, true);
1621
$countdownNumber = 5;
1722

1823
$loop = new Loop();
@@ -21,12 +26,16 @@
2126
echo "Countdown: $countdownNumber", PHP_EOL;
2227
$countdownNumber--;
2328
});
29+
$tinyTickTimer->addCallback(function() {
30+
echo ".";
31+
});
2432
$stopTimer->addCallback(function() use($loop) {
2533
echo "LIFT OFF!", PHP_EOL;
2634
$loop->halt();
2735
});
2836

2937
$loop->addTimer($periodicTimer);
38+
$loop->addTimer($tinyTickTimer);
3039
$loop->addTimer($stopTimer);
3140
echo "Starting...", PHP_EOL;
3241
$loop->run();

0 commit comments

Comments
 (0)