<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.ev.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'ev.periodic-modes.php',
    1 => 'Periodic watcher operation modes',
    2 => 'Periodic watcher operation modes',
  ),
  'up' => 
  array (
    0 => 'book.ev.php',
    1 => 'Ev',
  ),
  'prev' => 
  array (
    0 => 'ev.watcher-callbacks.php',
    1 => 'Watcher callbacks',
  ),
  'next' => 
  array (
    0 => 'class.ev.php',
    1 => 'Ev',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/ev/periodic-modes.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="ev.periodic-modes" class="chapter">
 <h1 class="title">Periodic watcher operation modes</h1>

 <p class="simpara">
  <span class="classname"><a href="class.evperiodic.php" class="classname">EvPeriodic</a></span>
  watcher works in different modes depending on the
  <code class="parameter">offset</code>,
  <code class="parameter">interval</code>
  and
  <code class="parameter">reschedule_cb</code>
  parameters.
 </p>
 <ol type="1">
  <li class="listitem">
   <span class="simpara">
    <em>Absolute timer</em>.
    In this mode
    <code class="parameter">interval</code>
    =
    <code class="literal">0</code>,
    <code class="parameter">reschedule_cb</code>
    = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>. This time simply fires at the wallclock time
    <code class="parameter">offset</code>
    and doesn&#039;t repeat. It will not adjust when a time jump occurs, that is,
    if it is to be run at
    <em>January
   1st 2014</em>
    then it will run when the system time reaches or surpasses this time.
   </span>
  </li>
  <li class="listitem">
   <span class="simpara">
    <em>Repeating interval timer</em>.
    In this mode
    <code class="parameter">interval</code>
    &gt;
    <code class="literal">0</code>,
    <code class="parameter">reschedule_cb</code>
    = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>; the watcher will always be scheduled to timeout at the next
    <code class="parameter">offset</code>
    +
    <strong><code>N</code></strong>
    *
    <code class="parameter">interval</code>
    time(for some integer
    <strong><code>N</code></strong>)
    and then repeat, regardless of any time jumps.
   </span>
   <p class="para">
    This can be used to create timers that do not drift with respect to system
    time:
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$hourly </span><span style="color: #007700">= </span><span style="color: #0000BB">EvPeriodic</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">3600</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, function () {<br />  echo </span><span style="color: #DD0000">"once per hour\n"</span><span style="color: #007700">;<br />});<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    That doesn&#039;t mean there will always be
    <code class="literal">3600</code>
    seconds in between triggers, but only that the callback will be called
    when the system time shows a full hour(
    <em>UTC</em>
    ).
   </p>
   <span class="simpara">
    <span class="classname"><a href="class.evperiodic.php" class="classname">EvPeriodic</a></span>
    will try to run the callback in this mode at the next possible time where
    <var class="varname">time</var>
    =
    <code class="parameter">offset</code>
    (
    <code class="literal">mod</code>
    <code class="parameter">interval</code>
    ), regardless of any time jumps.
   </span>
  </li>
  <li class="listitem">
   <span class="simpara">
    <em>Manual reschedule mode</em>.
    In this mode
    <code class="parameter">reschedule_cb</code>
    is a
    <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span>.
   </span>
   <span class="simpara">
    <code class="parameter">interval</code>
    and
    <code class="parameter">offset</code>
    are both being ignored. Instead, each time the periodic watcher gets
    scheduled, the reschedule callback (
    <code class="parameter">reschedule_cb</code>)
    will be called with the watcher as first, and the current time as second
    argument.
   </span>
   <span class="simpara">
    This callback
    <em>must not</em>
    stop or destroy this or any other periodic watchers, ever, and
    <em>must not</em>
    call any event loop functions or methods. To stop it return
    <code class="literal">1e30</code>
    and stop it afterwards. An
    <span class="classname"><a href="class.evprepare.php" class="classname">EvPrepare</a></span>
    watcher may be used for this task.
   </span>
   <span class="simpara">
    It must return the next time to trigger, based on the passed time value
    (that is, the lowest time value larger than or equal to the second
    argument). It will usually be called just before the callback will be
    triggered, but might be called at other times, too.
   </span>
   <div class="example" id="example-1">
    <p><strong>Example #1 Using reschedule callback</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Tick each 10.5 seconds<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">reschedule_cb </span><span style="color: #007700">(</span><span style="color: #0000BB">$watcher</span><span style="color: #007700">, </span><span style="color: #0000BB">$now</span><span style="color: #007700">) {<br />   return </span><span style="color: #0000BB">$now </span><span style="color: #007700">+ (</span><span style="color: #0000BB">10.5</span><span style="color: #007700">. - </span><span style="color: #0000BB">fmod</span><span style="color: #007700">(</span><span style="color: #0000BB">$now</span><span style="color: #007700">, </span><span style="color: #0000BB">10.5</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$w </span><span style="color: #007700">= new </span><span style="color: #0000BB">EvPeriodic</span><span style="color: #007700">(</span><span style="color: #0000BB">0.</span><span style="color: #007700">, </span><span style="color: #0000BB">0.</span><span style="color: #007700">, </span><span style="color: #DD0000">"reschedule_cb"</span><span style="color: #007700">, function (</span><span style="color: #0000BB">$w</span><span style="color: #007700">, </span><span style="color: #0000BB">$revents</span><span style="color: #007700">) {<br />   echo </span><span style="color: #0000BB">time</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </li>
 </ol>
</div>
<?php manual_footer($setup); ?>