blob: 1167d2cdc97b00be6c07df83d660c700de1e439e [file] [log] [blame]
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Timers Node.js v0.10.24 Manual &amp; Documentation</title>
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/sh.css">
<link rel="canonical" href="http://nodejs.org/api/timers.html">
</head>
<body class="alt apidoc" id="api-section-timers">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
<img id="logo" src="http://nodejs.org/images/logo-light.png" alt="node.js">
</a>
</div>
<div id="content" class="clearfix">
<div id="column2" class="interior">
<ul>
<li><a href="/" class="home">Home</a></li>
<li><a href="/download/" class="download">Download</a></li>
<li><a href="/about/" class="about">About</a></li>
<li><a href="http://npmjs.org/" class="npm">npm Registry</a></li>
<li><a href="http://nodejs.org/api/" class="docs current">Docs</a></li>
<li><a href="http://blog.nodejs.org" class="blog">Blog</a></li>
<li><a href="/community/" class="community">Community</a></li>
<li><a href="/logos/" class="logos">Logos</a></li>
<li><a href="http://jobs.nodejs.org/" class="jobs">Jobs</a></li>
</ul>
<p class="twitter"><a href="http://twitter.com/nodejs">@nodejs</a></p>
</div>
<div id="column1" class="interior">
<header>
<h1>Node.js v0.10.24 Manual &amp; Documentation</h1>
<div id="gtoc">
<p>
<a href="index.html" name="toc">Index</a> |
<a href="all.html">View on single page</a> |
<a href="timers.json">View as JSON</a>
</p>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li><a href="#timers_timers">Timers</a><ul>
<li><a href="#timers_settimeout_callback_delay_arg">setTimeout(callback, delay, [arg], [...])</a></li>
<li><a href="#timers_cleartimeout_timeoutid">clearTimeout(timeoutId)</a></li>
<li><a href="#timers_setinterval_callback_delay_arg">setInterval(callback, delay, [arg], [...])</a></li>
<li><a href="#timers_clearinterval_intervalid">clearInterval(intervalId)</a></li>
<li><a href="#timers_unref">unref()</a></li>
<li><a href="#timers_ref">ref()</a></li>
<li><a href="#timers_setimmediate_callback_arg">setImmediate(callback, [arg], [...])</a></li>
<li><a href="#timers_clearimmediate_immediateid">clearImmediate(immediateId)</a></li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>Timers<span><a class="mark" href="#timers_timers" id="timers_timers">#</a></span></h1>
<pre class="api_stability_5">Stability: 5 - Locked</pre><p>All of the timer functions are globals. You do not need to <code>require()</code>
this module in order to use them.
</p>
<h2>setTimeout(callback, delay, [arg], [...])<span><a class="mark" href="#timers_settimeout_callback_delay_arg" id="timers_settimeout_callback_delay_arg">#</a></span></h2>
<p>To schedule execution of a one-time <code>callback</code> after <code>delay</code> milliseconds. Returns a
<code>timeoutId</code> for possible use with <code>clearTimeout()</code>. Optionally you can
also pass arguments to the callback.
</p>
<p>It is important to note that your callback will probably not be called in exactly
<code>delay</code> milliseconds - Node.js makes no guarantees about the exact timing of when
the callback will fire, nor of the ordering things will fire in. The callback will
be called as close as possible to the time specified.
</p>
<h2>clearTimeout(timeoutId)<span><a class="mark" href="#timers_cleartimeout_timeoutid" id="timers_cleartimeout_timeoutid">#</a></span></h2>
<p>Prevents a timeout from triggering.
</p>
<h2>setInterval(callback, delay, [arg], [...])<span><a class="mark" href="#timers_setinterval_callback_delay_arg" id="timers_setinterval_callback_delay_arg">#</a></span></h2>
<p>To schedule the repeated execution of <code>callback</code> every <code>delay</code> milliseconds.
Returns a <code>intervalId</code> for possible use with <code>clearInterval()</code>. Optionally
you can also pass arguments to the callback.
</p>
<h2>clearInterval(intervalId)<span><a class="mark" href="#timers_clearinterval_intervalid" id="timers_clearinterval_intervalid">#</a></span></h2>
<p>Stops a interval from triggering.
</p>
<h2>unref()<span><a class="mark" href="#timers_unref" id="timers_unref">#</a></span></h2>
<p>The opaque value returned by <code>setTimeout</code> and <code>setInterval</code> also has the method
<code>timer.unref()</code> which will allow you to create a timer that is active but if
it is the only item left in the event loop won&#39;t keep the program running.
If the timer is already <code>unref</code>d calling <code>unref</code> again will have no effect.
</p>
<p>In the case of <code>setTimeout</code> when you <code>unref</code> you create a separate timer that
will wakeup the event loop, creating too many of these may adversely effect
event loop performance -- use wisely.
</p>
<h2>ref()<span><a class="mark" href="#timers_ref" id="timers_ref">#</a></span></h2>
<p>If you had previously <code>unref()</code>d a timer you can call <code>ref()</code> to explicitly
request the timer hold the program open. If the timer is already <code>ref</code>d calling
<code>ref</code> again will have no effect.
</p>
<h2>setImmediate(callback, [arg], [...])<span><a class="mark" href="#timers_setimmediate_callback_arg" id="timers_setimmediate_callback_arg">#</a></span></h2>
<p>To schedule the &quot;immediate&quot; execution of <code>callback</code> after I/O events
callbacks and before <code>setTimeout</code> and <code>setInterval</code> . Returns an
<code>immediateId</code> for possible use with <code>clearImmediate()</code>. Optionally you
can also pass arguments to the callback.
</p>
<p>Immediates are queued in the order created, and are popped off the queue once
per loop iteration. This is different from <code>process.nextTick</code> which will
execute <code>process.maxTickDepth</code> queued callbacks per iteration. <code>setImmediate</code>
will yield to the event loop after firing a queued callback to make sure I/O is
not being starved. While order is preserved for execution, other I/O events may
fire between any two scheduled immediate callbacks.
</p>
<h2>clearImmediate(immediateId)<span><a class="mark" href="#timers_clearimmediate_immediateid" id="timers_clearimmediate_immediateid">#</a></span></h2>
<p>Stops an immediate from triggering.
</p>
</div>
</div>
</div>
<div id="footer">
<a href="http://joyent.com" class="joyent-logo">Joyent</a>
<ul class="clearfix">
<li><a href="/">Node.js</a></li>
<li><a href="/download/">Download</a></li>
<li><a href="/about/">About</a></li>
<li><a href="http://npmjs.org/">npm Registry</a></li>
<li><a href="http://nodejs.org/api/">Docs</a></li>
<li><a href="http://blog.nodejs.org">Blog</a></li>
<li><a href="/community/">Community</a></li>
<li><a href="/logos/">Logos</a></li>
<li><a href="http://jobs.nodejs.org/">Jobs</a></li>
<li><a href="http://twitter.com/nodejs" class="twitter">@nodejs</a></li>
</ul>
<p>Copyright <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="/trademark-policy.pdf">trademark</a> of Joyent, Inc. View <a href="https://raw.github.com/joyent/node/v0.10.24/LICENSE">license</a>.</p>
</div>
<script src="../sh_main.js"></script>
<script src="../sh_javascript.min.js"></script>
<script>highlight(undefined, undefined, 'pre');</script>
<script>
window._gaq = [['_setAccount', 'UA-10874194-2'], ['_trackPageview']];
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.src = '//www.google-analytics.com/ga.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
</script>
</body>
</html>