OmG
0
Q:

how to wait in javascript

//code before the pause
setTimeout(function(){
    //do what you need here
}, 2000);
1
function pause(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

//Pause for 1000 milliseconds

pause(1000)
0
 wait(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
 }
10
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
2
timeout(ms) { //pass a time in milliseconds to this function
    return new Promise(resolve => setTimeout(resolve, ms));
  }
3
window.setTimeout(function, milliseconds);
0
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

console.log("Hello");
sleep(2000).then(() => { console.log("World!"); });
0
function firstFunction(_callback){
    // do some asynchronous work
    // and when the asynchronous stuff is complete
    _callback();    
}

function secondFunction(){
    // call first function and pass in a callback function which
    // first function runs when it has completed
    firstFunction(function() {
        console.log('huzzah, I\'m done!');
    });    
}
2
    async function delay(delayInms) {
      return new Promise(resolve  => {
        setTimeout(() => {
          resolve(2);
        }, delayInms);
      });
    }
    async function sample() {
      console.log('a');
      console.log('waiting...')
      let delayres = await delay(3000);
      console.log('b');
    }
    sample();
1

New to Communities?

Join the community