Javascript - Thread Wait ?

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript)

1 - About

JavaScript is a single-thread event-based model. Therefore, you don't want and can't stop the execution of code. May be with promise ?

3 - Wait

3.1 - Web API setTimeout

The setTimeOut is wrapper around code that create a job to execute it later. The execution of the thread (of the code) does not stop.

See Browser - setTimeOut

Advertising

3.2 - 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>

3.3 - JQuery

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

4 - Documentation / Reference