Update.
This commit is contained in:
+157
-20
@@ -42,39 +42,61 @@
|
||||
<li><a href="index.html">Overview</a></li>
|
||||
<li><a href="string.html">String</a></li>
|
||||
<li><a href="number.html">Num</a></li>
|
||||
<li><a href="http.html">http</a></li>
|
||||
<li><a href="websocket.html">websocket</a></li>
|
||||
<li><a href="tls.html">tls</a></li>
|
||||
<li><a href="net.html">net</a></li>
|
||||
<li><a href="dns.html">dns</a></li>
|
||||
<li><a href="json.html">json</a></li>
|
||||
<li><a href="argparse.html">argparse</a></li>
|
||||
<li><a href="base64.html">base64</a></li>
|
||||
<li><a href="regex.html">regex</a></li>
|
||||
<li><a href="jinja.html">jinja</a></li>
|
||||
<li><a href="crypto.html">crypto</a></li>
|
||||
<li><a href="os.html">os</a></li>
|
||||
<li><a href="env.html">env</a></li>
|
||||
<li><a href="signal.html">signal</a></li>
|
||||
<li><a href="subprocess.html">subprocess</a></li>
|
||||
<li><a href="sqlite.html">sqlite</a></li>
|
||||
<li><a href="dataset.html">dataset</a></li>
|
||||
<li><a href="datetime.html">datetime</a></li>
|
||||
<li><a href="timer.html" class="active">timer</a></li>
|
||||
<li><a href="dns.html">dns</a></li>
|
||||
<li><a href="env.html">env</a></li>
|
||||
<li><a href="fswatch.html">fswatch</a></li>
|
||||
<li><a href="html.html">html</a></li>
|
||||
<li><a href="http.html">http</a></li>
|
||||
<li><a href="io.html">io</a></li>
|
||||
<li><a href="pathlib.html">pathlib</a></li>
|
||||
<li><a href="scheduler.html">scheduler</a></li>
|
||||
<li><a href="jinja.html">jinja</a></li>
|
||||
<li><a href="json.html">json</a></li>
|
||||
<li><a href="markdown.html">markdown</a></li>
|
||||
<li><a href="math.html">math</a></li>
|
||||
<li><a href="net.html">net</a></li>
|
||||
<li><a href="os.html">os</a></li>
|
||||
<li><a href="pathlib.html">pathlib</a></li>
|
||||
<li><a href="regex.html">regex</a></li>
|
||||
<li><a href="scheduler.html">scheduler</a></li>
|
||||
<li><a href="signal.html">signal</a></li>
|
||||
<li><a href="sqlite.html">sqlite</a></li>
|
||||
<li><a href="subprocess.html">subprocess</a></li>
|
||||
<li><a href="sysinfo.html">sysinfo</a></li>
|
||||
<li><a href="tempfile.html">tempfile</a></li>
|
||||
<li><a href="timer.html" class="active">timer</a></li>
|
||||
<li><a href="tls.html">tls</a></li>
|
||||
<li><a href="udp.html">udp</a></li>
|
||||
<li><a href="uuid.html">uuid</a></li>
|
||||
<li><a href="wdantic.html">wdantic</a></li>
|
||||
<li><a href="web.html">web</a></li>
|
||||
<li><a href="websocket.html">websocket</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section">
|
||||
<span class="section-title">Tutorials</span>
|
||||
<ul>
|
||||
<li><a href="../tutorials/index.html">Tutorial List</a></li>
|
||||
<li><a href="../tutorials/http-client.html">HTTP Client</a></li>
|
||||
<li><a href="../tutorials/websocket-chat.html">WebSocket Chat</a></li>
|
||||
<li><a href="../tutorials/database-app.html">Database App</a></li>
|
||||
<li><a href="../tutorials/template-rendering.html">Templates</a></li>
|
||||
<li><a href="../tutorials/cli-tool.html">CLI Tool</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="section">
|
||||
<span class="section-title">How-To Guides</span>
|
||||
<ul>
|
||||
<li><a href="../howto/index.html">How-To List</a></li>
|
||||
<li><a href="../howto/http-requests.html">HTTP Requests</a></li>
|
||||
<li><a href="../howto/json-parsing.html">JSON Parsing</a></li>
|
||||
<li><a href="../howto/regex-patterns.html">Regex Patterns</a></li>
|
||||
<li><a href="../howto/file-operations.html">File Operations</a></li>
|
||||
<li><a href="../howto/async-operations.html">Async Operations</a></li>
|
||||
<li><a href="../howto/error-handling.html">Error Handling</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
@@ -91,9 +113,9 @@
|
||||
<article>
|
||||
<h1>timer</h1>
|
||||
|
||||
<p>The <code>timer</code> module provides timing functionality for delays and intervals.</p>
|
||||
<p>The <code>timer</code> module provides timing functionality for delays, intervals, and immediate execution.</p>
|
||||
|
||||
<pre><code>import "timer" for Timer</code></pre>
|
||||
<pre><code>import "timer" for Timer, TimerHandle</code></pre>
|
||||
|
||||
<h2>Timer Class</h2>
|
||||
|
||||
@@ -115,6 +137,63 @@
|
||||
Timer.sleep(1000) // Wait 1 second
|
||||
System.print("Done!")</code></pre>
|
||||
|
||||
<div class="method-signature">
|
||||
<span class="method-name">Timer.interval</span>(<span class="param">milliseconds</span>, <span class="param">fn</span>) → <span class="type">TimerHandle</span>
|
||||
</div>
|
||||
<p>Creates a repeating timer that calls the callback function at the specified interval. Returns a TimerHandle that can be used to stop the interval.</p>
|
||||
<ul class="param-list">
|
||||
<li><span class="param-name">milliseconds</span> <span class="param-type">(Num)</span> - Interval between calls in milliseconds</li>
|
||||
<li><span class="param-name">fn</span> <span class="param-type">(Fn)</span> - Callback function to execute (no arguments)</li>
|
||||
<li><span class="returns">Returns:</span> TimerHandle for controlling the interval</li>
|
||||
</ul>
|
||||
<pre><code>var count = 0
|
||||
var handle = Timer.interval(1000) {
|
||||
count = count + 1
|
||||
System.print("Tick %(count)")
|
||||
if (count >= 5) {
|
||||
handle.stop()
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<div class="method-signature">
|
||||
<span class="method-name">Timer.immediate</span>(<span class="param">fn</span>)
|
||||
</div>
|
||||
<p>Schedules a function to run on the next iteration of the event loop. Useful for deferring execution without blocking.</p>
|
||||
<ul class="param-list">
|
||||
<li><span class="param-name">fn</span> <span class="param-type">(Fn)</span> - Callback function to execute (no arguments)</li>
|
||||
</ul>
|
||||
<pre><code>System.print("Before")
|
||||
Timer.immediate {
|
||||
System.print("Deferred execution")
|
||||
}
|
||||
System.print("After")
|
||||
// Output: Before, After, Deferred execution</code></pre>
|
||||
|
||||
<h2>TimerHandle Class</h2>
|
||||
|
||||
<div class="class-header">
|
||||
<h3>TimerHandle</h3>
|
||||
<p>Handle for controlling interval timers</p>
|
||||
</div>
|
||||
|
||||
<h3>Methods</h3>
|
||||
|
||||
<div class="method-signature">
|
||||
<span class="method-name">stop</span>()
|
||||
</div>
|
||||
<p>Stops the interval timer. After calling stop, the callback will no longer be invoked.</p>
|
||||
<pre><code>handle.stop()</code></pre>
|
||||
|
||||
<h3>Properties</h3>
|
||||
|
||||
<div class="method-signature">
|
||||
<span class="method-name">isActive</span> → <span class="type">Bool</span>
|
||||
</div>
|
||||
<p>Returns true if the interval timer is still active.</p>
|
||||
<pre><code>if (handle.isActive) {
|
||||
System.print("Timer is running")
|
||||
}</code></pre>
|
||||
|
||||
<h2>Examples</h2>
|
||||
|
||||
<h3>Basic Delay</h3>
|
||||
@@ -187,15 +266,73 @@ for (step in 1..20) {
|
||||
}
|
||||
System.print("\rDone! ")</code></pre>
|
||||
|
||||
<h3>Interval Timer</h3>
|
||||
<pre><code>import "timer" for Timer, TimerHandle
|
||||
|
||||
var seconds = 0
|
||||
var handle = Timer.interval(1000) {
|
||||
seconds = seconds + 1
|
||||
System.print("Elapsed: %(seconds) seconds")
|
||||
}
|
||||
|
||||
Timer.sleep(5000)
|
||||
handle.stop()
|
||||
System.print("Timer stopped")</code></pre>
|
||||
|
||||
<h3>Periodic Status Updates</h3>
|
||||
<pre><code>import "timer" for Timer, TimerHandle
|
||||
import "sysinfo" for SysInfo
|
||||
|
||||
var handle = Timer.interval(2000) {
|
||||
var freeMB = (SysInfo.freeMemory / 1024 / 1024).floor
|
||||
var load = SysInfo.loadAverage[0]
|
||||
System.print("Memory: %(freeMB) MB free | Load: %(load)")
|
||||
}
|
||||
|
||||
System.print("Monitoring system (runs for 10 seconds)...")
|
||||
Timer.sleep(10000)
|
||||
handle.stop()</code></pre>
|
||||
|
||||
<h3>Immediate Execution</h3>
|
||||
<pre><code>import "timer" for Timer
|
||||
|
||||
System.print("1. Synchronous")
|
||||
|
||||
Timer.immediate {
|
||||
System.print("3. Immediate callback")
|
||||
}
|
||||
|
||||
System.print("2. Still synchronous")
|
||||
|
||||
Timer.sleep(100)</code></pre>
|
||||
|
||||
<h3>Self-Stopping Interval</h3>
|
||||
<pre><code>import "timer" for Timer, TimerHandle
|
||||
|
||||
var count = 0
|
||||
var handle = Timer.interval(500) {
|
||||
count = count + 1
|
||||
System.print("Count: %(count)")
|
||||
if (count >= 10) {
|
||||
System.print("Stopping at %(count)")
|
||||
handle.stop()
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<div class="admonition note">
|
||||
<div class="admonition-title">Note</div>
|
||||
<p>Timer.sleep is non-blocking at the event loop level. The current fiber suspends, but other scheduled operations can continue.</p>
|
||||
</div>
|
||||
|
||||
<div class="admonition tip">
|
||||
<div class="admonition-title">Tip</div>
|
||||
<p>Use <code>Timer.interval</code> for recurring tasks like heartbeats, polling, or animations. The returned handle allows you to stop the interval from within the callback itself.</p>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<footer class="page-footer">
|
||||
<a href="datetime.html" class="prev">datetime</a>
|
||||
<a href="io.html" class="next">io</a>
|
||||
<a href="tempfile.html" class="prev">tempfile</a>
|
||||
<a href="tls.html" class="next">tls</a>
|
||||
</footer>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user