<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.swoole.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'class.swoole-timer.php',
    1 => 'Swoole\\Timer',
    2 => 'The Swoole\\Timer class',
  ),
  'up' => 
  array (
    0 => 'book.swoole.php',
    1 => 'Swoole',
  ),
  'prev' => 
  array (
    0 => 'swoole-table.valid.php',
    1 => 'Swoole\\Table::valid',
  ),
  'next' => 
  array (
    0 => 'swoole-timer.after.php',
    1 => 'Swoole\\Timer::after',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/swoole/swoole.timer.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.swoole-timer.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.swoole-timer" class="reference">

 <h1 class="title">The Swoole\Timer class</h1>
 

 <div class="partintro"><p class="verinfo">(PECL swoole &gt;= 1.9.0)</p>


  <div class="section" id="swoole-timer.intro">
   <h2 class="title">Introduction</h2>
   <p class="simpara">
    Millisecond precision timer. The underlying implementation is based on epoll_wait and setitimer,
    using a min-heap data structure that supports adding a large number of timers.
   </p>
   <p class="simpara">
    In synchronous I/O processes, such as the Manager and TaskWorker processes, it is
    implemented using setitimer and signals.
   </p>
   <p class="simpara">
    In asynchronous I/O processes, it is implemented using the timeout of
    epoll_wait/kevent/poll/select.
   </p>
   <p class="para">
    The underlying system does not support timers with a delay of <code class="literal">0</code>;
    values below <code class="literal">1</code> millisecond emit an
    <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> and the call fails. This differs from
    languages such as Node.js.
    <span class="methodname"><a href="swoole-event.defer.php" class="methodname">Swoole\Event::defer()</a></span>
    may be used to achieve similar functionality.
    <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
Swoole\Event</span><span style="color: #007700">::</span><span style="color: #0000BB">defer</span><span style="color: #007700">(function () {
  echo </span><span style="color: #DD0000">"hello\n"</span><span style="color: #007700">;
});
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

   </p>
   <p class="simpara">
    Timer Correction: The execution time of the timer callback function does not affect the timing of the
    next timer execution. For example, setting a tick timer of 10ms after 0.002s, the first callback will
    be executed at 0.012s, if the callback function takes 5ms to execute, the next timer will still trigger
    at 0.022s, not at 0.027s.
   </p>
   <p class="simpara">
    However, if the execution time of the timer callback function is too long, even covering the time of the
    next timer execution, the underlying system will perform time correction, discarding the expired behavior
    and triggering the timer callback at the next available time. For example, if the callback function
    at 0.012s takes 15ms to execute, causing the timer at 0.022s to be delayed, the timer callback
    will be triggered again at 0.032s.
   </p>
   <p class="simpara">
    By default, when a timer is triggered, a coroutine is automatically created
    to execute the callback function. This can be disabled with
    <span class="function"><a href="function.swoole-async-set.php" class="function">swoole_async_set()</a></span>.
   </p>
   <div class="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     Timer only works within the current process space.
    </p>
   </div>
   <div class="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     Timer is purely asynchronous and incompatible with synchronous I/O functions.
    </p>
   </div>
   <div class="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     Timer execution may experience minor timing deviations.
    </p>
   </div>
  </div>


  <div class="section" id="swoole-timer.synopsis">
   <h2 class="title">Class synopsis</h2>


   <div class="classsynopsis">
    <span class="ooclass"><strong class="classname"></strong></span>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">class</span> <strong class="classname">Swoole\Timer</strong>
     </span>
     {</div>

    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.after.php" class="methodname">after</a></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$ms</code></span>, <span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$params</code></span>): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.clear.php" class="methodname">clear</a></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$timer_id</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.clearall.php" class="methodname">clearAll</a></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.exists.php" class="methodname">exists</a></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$timer_id</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.info.php" class="methodname">info</a></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$timer_id</code></span>): <span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.array.php" class="type array">array</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.list.php" class="methodname">list</a></span>(): <span class="type">Swoole\Timer\Iterator</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.set.php" class="methodname">set</a></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$settings</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.stats.php" class="methodname">stats</a></span>(): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="modifier">static</span> <span class="modifier">function</span> <span class="methodname"><a href="swoole-timer.tick.php" class="methodname">tick</a></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$ms</code></span>, <span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$params</code></span>): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

   }</div>


  </div>

 </div>

 































































<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="swoole-timer.after.php">Swoole\Timer::after</a> — Execute a function after a specified time</li><li><a href="swoole-timer.clear.php">Swoole\Timer::clear</a> — Delete a timer by ID</li><li><a href="swoole-timer.clearall.php">Swoole\Timer::clearAll</a> — Clear all timers in the current process.</li><li><a href="swoole-timer.exists.php">Swoole\Timer::exists</a> — Check if a timer is existed.</li><li><a href="swoole-timer.info.php">Swoole\Timer::info</a> — Get information about a timer.</li><li><a href="swoole-timer.list.php">Swoole\Timer::list</a> — Get an iterator over the timers of the current process</li><li><a href="swoole-timer.set.php">Swoole\Timer::set</a> — Set timer-related parameters</li><li><a href="swoole-timer.stats.php">Swoole\Timer::stats</a> — Get timer statistics.</li><li><a href="swoole-timer.tick.php">Swoole\Timer::tick</a> — Set a repeating interval timer</li></ul>
</div>
<?php manual_footer($setup); ?>