Skip to main content

\Co\repeat

###API

namespace Co;

function repeat(Closure $closure,int|float $second): string;

Parameter description​

ParametersTypeDescription
$closureClosureClosure function that is executed repeatedly, running in the event context

Closure parameters​

ParametersTypeDescription
$cancelClosureCallback 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 submit. The current repeated task can be canceled by calling the cancel 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, but repeat will still be executed again after 1 second.