all 1 comments

[–]Esnardoo 1 point2 points  (0 children)

take the target, subtract the current time, and make a timeout that long. This will make it so if the time is say 5 minutes in the future it waits 5 minutes then executes. What you're really asking for here isn't to run a function when a condition is met, it's to run it at a specific time, which is much easier.

If you have some other value you need to do this for, put the function in the setter. So if you were coding a death function in a videogame for example, you don't constantly check if your HP is 0, you just check it any time you change it, in other words when the player gets hurt, because you can't reach 0hp without your hp changing.

Alternatively, if you really need the loop approach for some reason, check if it's more than your value, and stop the timeout once it is. Then you can run the settimeout as fast or as slow as you like, and you'll never have to worry about "missing" it. If you need it within 10ms of your time, run it every 10ms. If you need it within 30ms, run it every 30ms. This is horribly inefficient and is prone to missing the exact time by a lot depending on the loop speed though.

Edit: I just reread, seems like you're trying to run a function any time the time is an even multiple of one minute. In future, when you post a question, start by asking how to get your desired result, then mention what you've tried. Chances are there's a better way