<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Constructs an EvTimer watcher object</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="evtimer.again.html">EvTimer::again</a></div> <div class="next" style="text-align: right; float: right;"><a href="evtimer.createstopped.html">EvTimer::createStopped</a></div> <div class="up"><a href="class.evtimer.html">EvTimer</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="evtimer.construct" class="refentry"> <div class="refnamediv"> <h1 class="refname">EvTimer::__construct</h1> <p class="verinfo">(PECL ev >= 0.2.0)</p><p class="refpurpose"><span class="refname">EvTimer::__construct</span> — <span class="dc-title">Constructs an EvTimer watcher object</span></p> </div> <div class="refsect1 description" id="refsect1-evtimer.construct-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="methodname"><strong>EvTimer::__construct</strong></span> ( <span class="methodparam"> <span class="type">double</span> <code class="parameter">$after</code> </span> , <span class="methodparam"> <span class="type">double</span> <code class="parameter">$repeat</code> </span> , <span class="methodparam"> <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code> </span> [, <span class="methodparam"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$data</code> <span class="initializer"> = <strong><code>NULL</code></strong></span> </span> [, <span class="methodparam"> <span class="type">int</span> <code class="parameter">$priority</code> <span class="initializer"> = 0</span> </span> ]] )</div> <p class="para rdfs-comment"> Constructs an EvTimer watcher object. </p> </div> <div class="refsect1 parameters" id="refsect1-evtimer.construct-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"> <em><code class="parameter">after</code></em> </span> <dd> <p class="para"> Configures the timer to trigger after <em><code class="parameter">after</code></em> seconds. </p> </dd> </dt> <dt> <span class="term"> <em><code class="parameter">repeat</code></em> </span> <dd> <p class="para"> If repeat is <strong><code>0.0</code></strong> , then it will automatically be stopped once the timeout is reached. If it is positive, then the timer will automatically be configured to trigger again every repeat seconds later, until stopped manually. </p> </dd> </dt> <dt> <span class="term"> <em><code class="parameter">callback</code></em> </span> <dd> <p class="para"> See <a href="ev.watcher-callbacks.html" class="link">Watcher callbacks</a> . </p> </dd> </dt> <dt> <span class="term"> <em><code class="parameter">data</code></em> </span> <dd> <p class="para"> Custom data associated with the watcher. </p> </dd> </dt> <dt> <span class="term"> <em><code class="parameter">priority</code></em> </span> <dd> <p class="para"> <a href="class.ev.html#ev.constants.watcher-pri" class="link">Watcher priority</a> </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-evtimer.construct-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns EvTimer object on success. </p> </div> <div class="refsect1 examples" id="refsect1-evtimer.construct-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-3633"> <p><strong>Example #1 Simple timers</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Create and start timer firing after 2 seconds<br /></span><span style="color: #0000BB">$w1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, function () {<br /> echo </span><span style="color: #DD0000">"2 seconds elapsed\n"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #FF8000">// Create and launch timer firing after 2 seconds repeating each second<br />// until we manually stop it<br /></span><span style="color: #0000BB">$w2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, function (</span><span style="color: #0000BB">$w</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"is called every second, is launched after 2 seconds\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"iteration = "</span><span style="color: #007700">, </span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">// Stop the watcher after 5 iterations<br /> </span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">() == </span><span style="color: #0000BB">5 </span><span style="color: #007700">and </span><span style="color: #0000BB">$w</span><span style="color: #007700">-></span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br /> </span><span style="color: #FF8000">// Stop the watcher if further calls cause more than 10 iterations<br /> </span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">() >= </span><span style="color: #0000BB">10 </span><span style="color: #007700">and </span><span style="color: #0000BB">$w</span><span style="color: #007700">-></span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #FF8000">// Create stopped timer. It will be inactive until we start it ourselves<br /></span><span style="color: #0000BB">$w_stopped </span><span style="color: #007700">= </span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">::</span><span style="color: #0000BB">createStopped</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, function(</span><span style="color: #0000BB">$w</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Callback of a timer created as stopped\n"</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">// Stop the watcher after 2 iterations<br /> </span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">() >= </span><span style="color: #0000BB">2 </span><span style="color: #007700">and </span><span style="color: #0000BB">$w</span><span style="color: #007700">-></span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #FF8000">// Loop until Ev::stop() is called or all of watchers stop<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Start and look if it works<br /></span><span style="color: #0000BB">$w_stopped</span><span style="color: #007700">-></span><span style="color: #0000BB">start</span><span style="color: #007700">();<br />echo </span><span style="color: #DD0000">"Run single iteration\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">RUN_ONCE</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Restart the second watcher and try to handle the same events, but don't block\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$w2</span><span style="color: #007700">-></span><span style="color: #0000BB">again</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">RUN_NOWAIT</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$w </span><span style="color: #007700">= new </span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, function() {});<br />echo </span><span style="color: #DD0000">"Running a blocking loop\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br />echo </span><span style="color: #DD0000">"END\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 2 seconds elapsed is called every second, is launched after 2 seconds iteration = 1 is called every second, is launched after 2 seconds iteration = 2 is called every second, is launched after 2 seconds iteration = 3 is called every second, is launched after 2 seconds iteration = 4 is called every second, is launched after 2 seconds iteration = 5 Run single iteration Callback of a timer created as stopped Restart the second watcher and try to handle the same events, but don't block Running a blocking loop is called every second, is launched after 2 seconds iteration = 8 is called every second, is launched after 2 seconds iteration = 9 is called every second, is launched after 2 seconds iteration = 10 END </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-evtimer.construct-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"> <span class="methodname"><a href="evtimer.createstopped.html" class="methodname" rel="rdfs-seeAlso">EvTimer::createStopped()</a> - Creates EvTimer stopped watcher object</span> </li> <li class="member"> <a href="class.evperiodic.html" class="classname">EvPeriodic</a> </li> <li class="member"> <a href="http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_timer_code_relative_and_opti" class="link external">» ev_timer - relative and optionally repeating timeouts</a> </li> <li class="member"> <a href="http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts" class="link external">» Be smart about timeouts</a> </li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="evtimer.again.html">EvTimer::again</a></div> <div class="next" style="text-align: right; float: right;"><a href="evtimer.createstopped.html">EvTimer::createStopped</a></div> <div class="up"><a href="class.evtimer.html">EvTimer</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>