3 Ways To Pause Javascript (Simple Examples)

Welcome to a quick tutorial on how to pause in Javascript. Need to do a wait or delay a function in Javascript? The bad news, there are no native implementations of pause in Javascript. But we can emulate the behavior via various means – Read on for the examples!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes JS Pause Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

JAVASCRIPT PAUSE

All right, let us get into the examples of possible ways to pause in Javascript.

 

METHOD 1) TIMESTAMP WHILE LOOP

1-timer.js
function demo () {
  // (A) DO SOMETHING
  console.log("First");
 
  // (B) WAIT 1 SECOND
  let now = Date.now(),
      end = now + 1000;
  while (now < end) { now = Date.now(); }
 
  // (C) PROCEED
  console.log("Second");
}

For those who are lost:

  • We set let end = 1 second from now.
  • while (now < end) { now = Date.now(); } will loop endlessly until 1 second from now, effectively emulating a “pause”.

Yep, this is kind of dumb, but it works.

 

 

METHOD 2) FUNCTION TIMEOUT

2-timeout.html
function demo () {
  // (A) DO SOMETHING
  console.log("First");
 
  // (B) RUN THIS AFTER 1 SECOND
  setTimeout(demoA, 1000);
 
  // (C) NOTE - SETTIMEOUT() IS ASYNC
  // THIS WILL CONTINUE TO RUN!
  console.log("Third");
}

function demoA () { console.log("Second"); }

What is the next easiest way to emulate a “delay”? Just set the function in a good old timer – setTimeout(FUNCTION, MS). But please take note that setTimeout() is asynchronous, all blocks of code below it will continue to run.

 

METHOD 3) ASYNCHRONOUS WITH TIMEOUT

3-async-timeout.js
async function demo () {
  // (A) DO SOMETHING
  console.log("First");
 
  // (B) WAIT 1 SECOND
  await new Promise(res => { setTimeout(res, 1000); });
 
  // (C) PROCEED
  console.log("Second");
}

Lastly, this is a slightly better modern-day solution. This should be pretty self-explanatory even if you have yet to touch on asynchronous functions – await new Promise(res => { setTimeout(res, 1000); }); will wait for 1 second before proceeding.

 

 

USEFUL BITS & LINKS

That’s all for this guide, and here is a small section on some extras and links that may be useful to you.

 

WHICH IS THE BEST METHOD?

If I am forced to choose one – Async timeout. But otherwise, none of these are good solutions. Ever wonder why the developers of Javascript didn’t bother to implement a “sleep” or “pause” function? Definitely not because they have forgotten, but because it does not make sense. If you want to do things like:

  • Pause for a few seconds until the animation has ended.
  • Wait for the processing to end.
  • Loop and pause until things have been loaded.

Please don’t, Javascript is event-driven. Just attach an addEventListener() and you are better off than using an “emulated pause”.

 

LINKS & REFERENCES

 

 

INFOGRAPHIC CHEAT SHEET

How To Pause Or Wait In Javascript (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.