I wish you all having a great day. I am working on this project for a game and I want in case of winning that it waits for 2 seconds before returning to the start point I using this code to do just
function sleep(delay) {
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
which I got from this post
anyhow , the problem with this is that the game freziz for the number seconds
you can say in this picutre
My question is is there is more effichent way to do so I have tryied also
setTimeOut()
but it's the same problem
EDIT : This Is the Solution is great and work perfectly, Thanks a lot from this post
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
sleep(500).then(() => {
// Do something after the sleep!
});