Logo

Programming-Idioms

History of Idiom 45 > diff from v24 to v25

Edit summary for version 25 by :

Version 24

2015-10-27, 03:06:57

Version 25

2015-10-29, 14:05:14

Idiom #45 Pause execution for 5 seconds

Sleep for 5 seconds in current thread, before proceeding with next instructions.

Idiom #45 Pause execution for 5 seconds

Sleep for 5 seconds in current thread, before proceeding with next instructions.

Code
IO.puts "hello"
:timer.sleep(5000)
IO.puts "world"
Code
IO.puts "hello"
:timer.sleep(5000)
IO.puts "world"
Comments bubble
Elixir can leverage Erlang's stdlib, one of them is the :timer module, which has a sleep(milliseconds) function.
Comments bubble
Elixir can leverage Erlang's stdlib, one of them is the :timer module, which has a sleep(milliseconds) function.
Demo URL
http://elixirplayground.com?gist=b7e05b38a5263b46ff72
Demo URL
http://elixirplayground.com?gist=b7e05b38a5263b46ff72
Imports
import "dart:async";
Imports
import "dart:async";
Code
await new Future.delayed(const Duration(seconds : 5));
Code
await new Future.delayed(const Duration(seconds : 5));
Comments bubble
Waiting makes sense in asynchronous code. It's not done that often, and there is no simple primitive for it.

This will not pause the *thread*, but will delay the rest of the current async function.
Comments bubble
Waiting makes sense in asynchronous code. It's not done that often, and there is no simple primitive for it.

This will not pause the *thread*, but will delay the rest of the current async function.
Imports
import 'dart:io';
Imports
import 'dart:io';
Code
sleep(const Duration(seconds: 5));
Code
sleep(const Duration(seconds: 5));
Comments bubble
sleep is only available if dart:io is available (ie. on server side)
Comments bubble
sleep is only available if dart:io is available (ie. on server side)
Imports
import core.thread;
Imports
import core.thread;
Code
Thread.sleep(5.seconds);
Code
Thread.sleep(5.seconds);
Code
setTimeout(function(){
	// Instructions after delay
},5000);
Code
setTimeout(function(){
	// Instructions after delay
},5000);
Comments bubble
Javascript does not have a sleep function. The execution flow must be structured with a callback (it can be a closure).
Unit is millisecond.
Comments bubble
Javascript does not have a sleep function. The execution flow must be structured with a callback (it can be a closure).
Unit is millisecond.
Doc URL
https://developer.mozilla.org/en/docs/Web/API/WindowTimers/setTimeout
Doc URL
https://developer.mozilla.org/en/docs/Web/API/WindowTimers/setTimeout
Imports
import "time"
Imports
import "time"
Code
time.Sleep(5 * time.Second)
Code
time.Sleep(5 * time.Second)
Comments bubble
Unit is Duration, an alias for int64 representing a number of nanoseconds.
But the constant Second helps readability.
Comments bubble
Unit is Duration, an alias for int64 representing a number of nanoseconds.
But the constant Second helps readability.
Doc URL
http://golang.org/pkg/time/#Sleep
Doc URL
http://golang.org/pkg/time/#Sleep
Demo URL
http://play.golang.org/p/q_MCP3SHWG
Demo URL
http://play.golang.org/p/q_MCP3SHWG