Add time.Sleep for timers

Signed-off-by: Abhilash Gnan <abhilash.gnan@gmail.com>
This commit is contained in:
Abhilash Gnan 2019-12-14 00:24:43 +05:30
parent 43825687bb
commit 95c4c1f691
3 changed files with 21 additions and 5 deletions

View File

@ -38,4 +38,7 @@ func main() {
if stop2 {
fmt.Println("Timer 2 stopped")
}
// Give timer2 enough time if it has to expire.
time.Sleep(2)
}

View File

@ -1,2 +1,2 @@
e8e501d6083bea786629ca5e485e8b18caab4815
pLnKEIesooU
c7157df6604e894d7660af0a28442652e7bc2cd6
969wzKNNiWA

19
public/timers generated
View File

@ -45,7 +45,7 @@ at <a href="tickers">tickers</a>.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/pLnKEIesooU"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<a href="http://play.golang.org/p/969wzKNNiWA"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
@ -119,7 +119,7 @@ that you can cancel the timer before it expires.
Here&rsquo;s an example of that.</p>
</td>
<td class="code">
<td class="code leading">
<div class="highlight"><pre> <span class="nx">timer2</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">NewTimer</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">)</span>
<span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
@ -130,6 +130,19 @@ Here&rsquo;s an example of that.</p>
<span class="k">if</span> <span class="nx">stop2</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;Timer 2 stopped&quot;</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Give timer2 enough time if it has to expire</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">time</span><span class="p">.</span><span class="nx">Sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
@ -170,7 +183,7 @@ a chance to expire.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' timer1 := time.NewTimer(2 * time.Second)\u000A');codeLines.push(' \x3C-timer1.C\u000A fmt.Println(\"Timer 1 expired\")\u000A');codeLines.push(' timer2 := time.NewTimer(time.Second)\u000A go func() {\u000A \x3C-timer2.C\u000A fmt.Println(\"Timer 2 expired\")\u000A }()\u000A stop2 := timer2.Stop()\u000A if stop2 {\u000A fmt.Println(\"Timer 2 stopped\")\u000A }\u000A}\u000A');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' timer1 := time.NewTimer(2 * time.Second)\u000A');codeLines.push(' \x3C-timer1.C\u000A fmt.Println(\"Timer 1 expired\")\u000A');codeLines.push(' timer2 := time.NewTimer(time.Second)\u000A go func() {\u000A \x3C-timer2.C\u000A fmt.Println(\"Timer 2 expired\")\u000A }()\u000A stop2 := timer2.Stop()\u000A if stop2 {\u000A fmt.Println(\"Timer 2 stopped\")\u000A }\u000A');codeLines.push(' time.Sleep(2)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>