Reset an NSTimer's firing time to be from now instead of the last fire

前端 未结 5 576
面向向阳花
面向向阳花 2021-01-02 13:33

I have a NSTimer that fires with an interval of 3 seconds to decrease a value. When I do an action that increases that value, I want to restart the timer to cou

5条回答
  •  甜味超标
    2021-01-02 13:53

    Yes, you can invalidate it. And create it again.

    You can also use:

    - (void) myTimedTask {
        // other stuff this task needs to do
    
        // change the variable varyingDelay to be 1s or 3s or...  This can be an instance variable or global that is changed by other parts of your app
        // weStillWantTimer is also a similar variable that your app uses to stop this recurring task
    
    if (weStillWantTimer)
           [self performSelector:@selector(myTimedTask:) withObject:gameColor afterDelay:varyingDelay];
    
     }
    

    You call myTimedTask to start the recurring task. Once it is started, you can change the delay with varyingDelay or stop it with weStillWantTimer.

提交回复
热议问题