Call the Timer method once:
MyTimer = [Nstimer scheduledtimerwithtimeinterval:1.5 target:self selector: @selector (Scrolltimer) Userinfo:nil Repeats:no]; //
To invoke the Timer method repeatedly:
Timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (function:) Userinfo:nil Repeats:yes]; // run a function method every 1 seconds
Note: When you set the counter's repeats to Yes, the reference count of self is incremented by 1. as a result, self (that is, Viewcontroller) cannot be released, so the counter timer must be stopped at viewwillappear time, otherwise the memory leak may result.
Stop the timer from running, but this is a permanent stop: (note: after the stop, be sure to empty the timer, otherwise there is no release .) Don't believe me? You try it yourself ~)
// Cancel Timer [timer invalidate];
To achieve this: stop first, and then in some cases turn on the timer again, you can use the following method:
First off the timer cannot use the method above, should use the following method:
// Turn off the timer
Then you can use the following method to open this timer again:
// turn on timer
Example: For example, when the page disappears, turn off the timer, and then when the page opens again, turn on the timer.
(primarily to prevent it from running in the background, take up the CPU) can be implemented using the following code:
// the page will enter the foreground, turn on the timer -(void) viewwillappear: (BOOL) animated { // Open timer [ Scrollview.mytimer setfiredate:[nsdate distantpast]; } // The page disappears, goes into the background does not display this page, closes the timer -(void) viewdiddisappear: (BOOL) animated { // off timer [Scrollview.mytimer setfiredate:[nsdate distantfuture]; }
Enable and disable Timer Nstimer in iOS