File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * This example shows how an IndividualTimer can be used to schedule some ticks
4+ * in the future. Here, the callback function simply calculates how many seconds
5+ * have passed since the script's start and echos. You can see the correlation
6+ * with the output to the three trigger times that are added after creating
7+ * the IndividualTimer object.
8+ */
9+ require ("../vendor/autoload.php " );
10+
11+ $ timeAtScriptStart = microtime (true );
12+
13+ $ timer = new \Gt \Async \Timer \IndividualTimer ();
14+ $ timer ->addTriggerTime ($ timeAtScriptStart + 1 );
15+ $ timer ->addTriggerTime ($ timeAtScriptStart + 5 );
16+ $ timer ->addTriggerTime ($ timeAtScriptStart + 10 );
17+
18+ $ timer ->addCallback (function () use ($ timeAtScriptStart ) {
19+ $ now = microtime (true );
20+ $ secondsPassed = round ($ now - $ timeAtScriptStart );
21+ echo "Number of seconds passed: $ secondsPassed " , PHP_EOL ;
22+ });
23+
24+ $ loop = new \Gt \Async \Loop ();
25+ $ loop ->addTimer ($ timer );
26+ echo "Starting... " , PHP_EOL ;
27+ $ loop ->run ();
Original file line number Diff line number Diff line change 22namespace Gt \Async \Timer ;
33
44class IndividualTimer extends Timer {
5- public function __construct (float $ triggerTime ) {
5+ public function __construct (float $ triggerTime = null ) {
66 parent ::__construct ();
7- $ this ->addTriggerTime ($ triggerTime );
7+
8+ if (!is_null ($ triggerTime )) {
9+ $ this ->addTriggerTime ($ triggerTime );
10+ }
811 }
912
1013 public function addTriggerTime (float $ triggerTime ):void {
You can’t perform that action at this time.
0 commit comments