\Co\repeat
β οΈ This page was initialized by AI translation and may contain outdated or inaccurate information. If there are inaccuracies, please submit changes to correct these errors Correct
APIβ
namespace Co;
function repeat(Closure $closure,int|float $second): string;
Parameter Descriptionβ
Parameters | Type | Description |
---|---|---|
$closure | Closure | Closure function that is executed repeatedly, running in the event context |
Closure parametersβ
Parameters | Type | Description |
---|---|---|
$cancel | Closure | Callback function to cancel the current recurring task |
Overviewβ
Repeat (repeated execution), specify the frequency to do something repeatedly, usually used in scenarios such as scheduled tasks. ripple will provide a
cancel
callback function for the closure function you submitted. The current repeated task can be canceled by calling thecancel
function.
Basic usageβ
\Co\repeat(function (Closure $cancel) {
echo 'delay task';
if(rand(1, 10) === 10){
$cancel();
}
}, 1);
\Co\wait(); // Wait for all events to complete
Note: The repeat
method will repeatedly execute the closure function within the specified time interval until
the cancel
function is called.
\Co\repeat(function (Closure $cancel) {
\Co\sleep(10);
echo 'delay task';
$cancel();
}, 1);
\Co\wait();
In the above example, the code in repeat will be executed after 1 second, and when encountering
\Co\sleep
, the current coroutine will be automatically suspended, butrepeat
will still be executed again after 1 second.