Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

Idiom #45 Pause execution for 5 seconds

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

The pedestrian walks, then stays still for 5 seconds, then resumes walking
setTimeout(function(){
	// Instructions after delay
},5000);

Javascript does not have a sleep function. This execution flow is structured with a callback (it can be a closure).
Unit is millisecond.
await new Promise(r => setTimeout(r, 5000));

Can be used only inside an async function!
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

await sleep(5000);

Can be used only inside an async function!
delay 5.0;

New implementation...
< >
programming-idioms.org