Table of Contents

About

JavaScript is a single-thread event-loop based model.

By default, all JavaScript applications (whether they be Node.js, Deno, or Bun) run on a single operating system thread.

They won’t benefit from running with multiple cores. Node.js has a cluster module that can be used to address this.

Wait

Web API setTimeout

The setTimeOut is a function that will schedule the code execution later on the event loop. The execution of the thread (of the code) does not stop.

See How to use the setTimeOut javascript function (in the Browser)?

Promise

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo() {
  console.log('Taking a break...');
  await sleep(2000);
  console.log('Two second later');
}

var button = document.getElementById("startButton");
button.addEventListener("click", function () { demo() }, false);
<button id="startButton">Start Demo</button>

JQuery

$('#warning')
.addClass('highlight')
.delay(1000)
.removeClass('highlight');