In this video I’m going to show you how you can add a sleep function in JavaScript. Languages like .net, python and java have a method called sleep. This allows a method to pause and wait for a certain amount of time and then continue execution of its code. JavaScript does provide a setTimeout method where you can pass a function to execute after a certain amount of time. However with the addition of async and await in javascript we can write this solution in a more elegant way.
This is the solution used in the video:
Angle brackets are not allowed in the youtube description make sure to add it in the arrow function below
async function sleep(seconds) {
return new Promise((resolve) =rightAngleBracketHere setTimeout(resolve, seconds * 1000));
}
Смотрите видео How to make a sleep function in JavaScript with async await онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Coding With Adam 02 Март 2022, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 10,851 раз и оно понравилось 154 людям.