I have a scheduler that runs at a 1/60 interval. The problem is that inside the tick method that is under this schedule I have an if statement that triggers every 5 seconds. In doing so when the 5 second mark comes up, it gets triggered many times as long as the timer is at the 5 second mark. What I want to do is once the 5 second mark comes up the if statement goes through only once instead of multiple times since with the 1/60 interval, the tick method gets called several times while having the time as 5. What is the best solution without having another timer. Thanks in advance.
Schedule and Timer problem
(3 posts) (3 voices)-
Posted 2 years ago #
-
I would just have a variable to keep the value of time when it last went through that IF statement.
ie, in your header
float lastTimeExecuted;in your tick
//check if it's at your 5 second mark if(...) { //check if lastTimeExecuted is not equal to the current time value //if it's equal we bail and do nothing, if it's not equal we run through the set of //operations you need if(lastTimeExecuted!=currentTime) { //your code ... //and set lastTimeExecuted to currentTime, so next tick it won't meet the IF //condition and won't fire again lastTimeExecuted=currentTime; } }But maybe I misunderstood your questions?
Also, how are you checking the 5 second mark?Posted 2 years ago # -
Use another timer that runs once every 5 seconds. There is no performance loss and you don't have to play games. What is your reason for avoiding another timer?
Otherwise patrickC's solution works.
Posted 2 years ago #
Reply
You must log in to post.