del
boolean \Workerman\Timer::del(int $timer_id)
Delete a specific timer
Parameters
timer_id
The id of the timer, which is an integer returned by the add interface.
Return Value
boolean
Example
use Workerman\Worker;
use Workerman\Timer;
require_once __DIR__ . '/vendor/autoload.php';
$task = new Worker();
// How many processes to start for the scheduled task, be aware of the multi-process concurrency issue
$task->count = 1;
$task->onWorkerStart = function(Worker $task)
{
// Run every 2 seconds
$timer_id = Timer::add(2, function()
{
echo "task run\n";
});
// Run a one-time task after 20 seconds to delete the timer that runs every 2 seconds
Timer::add(20, function($timer_id)
{
Timer::del($timer_id);
}, array($timer_id), false);
};
// Run worker
Worker::runAll();
Example (Deleting the current timer within the timer callback)
use Workerman\Worker;
use Workerman\Timer;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$task = new Worker();
$task->onWorkerStart = function(Worker $task)
{
// Note that to use the current timer id within the callback, it must be referenced using (&)
$timer_id = Timer::add(1, function()use(&$timer_id)
{
static $i = 0;
echo $i++."\n";
// Delete the timer after running 10 times
if($i === 10)
{
Timer::del($timer_id);
}
});
};
// Run worker
Worker::runAll();