0

I would like to wait some time, execute one method and go on. Some one similar to:

setTimeout(()=> someMethod());

The problem is that the process goes on before the method is executed.

Could you help me?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
mgp1994
  • 59
  • 1
  • 7
  • Wait 5 seconds. setTimeout(()=> someMethod(), 5e3); – Tom Shaw Jan 26 '20 at 21:43
  • Does this answer your question? [Is there an equivalent Javascript or Jquery sleep function?](https://stackoverflow.com/questions/1277070/is-there-an-equivalent-javascript-or-jquery-sleep-function) – Heretic Monkey Jan 26 '20 at 22:01

1 Answers1

1

You can wait on a promise as followed:

const wait5s = () => {
  return new Promise(resolve => {
    setTimeout(() => resolve(), 5000)
  })
}

async function myFunc() {
  await wait5s()
  // Call you method here
  console.log('Hello World!')
}

myFunc()

Hope that answers your question.

Andrew Bui
  • 161
  • 3