0

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.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Meow Guy
  • 11
  • 2
  • Does this answer your question? [Alternatives to setTimeout for node.js](https://stackoverflow.com/questions/39877773/alternatives-to-settimeout-for-node-js) – Kinglish Jul 14 '21 at 08:20
  • "*ive also considered using Date.now() and doing a check every second to see if x amount of time has passed but thats very unrealistic in my use case*" that would likely be the correct approach here. Only, you'd need to use `setTimeout`/`setInterval` to schedule the tasks, instead of making a busy loop. But it's hard to give concrete suggestion without knowing what the use case is. – VLAZ Jul 14 '21 at 08:24
  • I'd also recommend to take a look at [this gist](https://gist.github.com/jakearchibald/cb03f15670817001b1157e62a076fe95) – Constantin Groß Jul 14 '21 at 08:25
  • "*this solution needs to work even if my process is killed then reopened.*" sounds even simpler. `setInterval(checkForReminders, 1000)` or similar and `checkForReminders` will check if there is any reminder scheduled for "now"-ish and display it. – VLAZ Jul 14 '21 at 08:30
  • @VLAZ while in theory this will work just fine this is only half of the problem, the other half is the actual reminder – Meow Guy Jul 14 '21 at 08:32
  • @MeowGuy that sounds like a separate thing from just delaying something. It's actually *what* to delay. – VLAZ Jul 14 '21 at 08:36
  • @VLAZ maybe i worded that wrong, i already have the remind function, but how will the code know when to activate it – Meow Guy Jul 14 '21 at 08:37
  • @MeowGuy something like `allReminders.filter(r => !r.alreadyShown).filter(r => (Date.now() - r.time) > 0).forEach(r => showReminder(r)`. Or whatever makes sense for your reminders, I am guessing what data you have for them. Point is, find the ones that *should be shown* and show them. How you define this is up to you. In general, taking the difference between two times can tell you whether there are 15 second left or it's 1 second past. Or any amount of time. Getting more than zero means the reminder is in the past. – VLAZ Jul 14 '21 at 08:44
  • Since it seems that you need a persistent solution, I recommend exploring [agenda](https://www.npmjs.com/package/agenda), [bull](https://www.npmjs.com/package/bull) or [bee-queue](https://www.npmjs.com/package/bee-queue). – daylily Jul 14 '21 at 09:06

2 Answers2

0

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
Sarkar
  • 1,225
  • 7
  • 21
0

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);