Answer the question
In order to leave comments, you need to log in
How to avoid an infinite loop?
Actually, my question is that the solution in the form of an infinite loop looks very unoptimized and ugly or something. Let's say I need to implement an alarm clock. I won't check the time every half second, will I? No other solution comes to mind. It is necessary that when a certain time is reached, the script itself is activated.
Answer the question
In order to leave comments, you need to log in
For example, you can use a timer, after specifying the time at what time to wake up, we calculate how long this timer should work and start it. As soon as the timer has worked, we wake up the person.
There will still be an infinite loop, either you write it or the library organizes it (some asynchronous framework like react, if your answer can be found in it).
In each specific case, you will need to implement it correctly in your own way.
With the clock, everything is simple - you need to check the time more than once a second, but take the shortest interval and calculate how long to wait before it (action time minus the current time) and simply set sleep with one command.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question