Here's my situation, my goal is to have a function execute after x amount of time, now setTimeout()
works cool for that except it unreliable in my use case, I've also considered using Date.now()
and doing a check every second to see if x amount of time has passed but that's very unrealistic in my use case also, I'm willing to try any suggestions!
I'd like to add that my use case is setting a reminder from a function that I would have made, I've got access to a key-value database and this solution needs to work even if my process is killed then reopened.
you can make a delay function and await it before execution of the next line
function Delay(seconds){
return new Promise(resolve => {
setTimeout(() => {
resolve(true)
}, seconds * 1000) // convert seconds to ms
})
}
now you can use it like this
//some code here
await Delay(5) // wait 5 seconds
// other codes here
It may be helpful for you
function setTime(milisec){
var startTime =new Date().getTime();
for(var i=0; i<milisec; i++){
if((new Date().getTime()-startTime) > milisec)
break;
}
console.log('Hii I am executed');
}
setTime(10000000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.