Timer Nstimer in iOS

Source: Internet
Author: User

Call the Timer method once:

[CPP]View Plain Copy
  1. mytimer = [nstimer scheduledtimerwithtimeinterval:1.5 target: Self selector: @selector (Scrolltimer)  userinfo:nil repeats:no];  
  2. //Do not repeat, call only once. Timer runs once and stops automatically

To invoke the Timer method repeatedly:

[CPP]View Plain Copy
  1. Timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (function:) Userinfo:nil repeats: YES];
  2. //Run the function method once 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 ~)

[CSharp]View Plain Copy
  1. //Cancel Timer   
  2. [Timer invalidate];
  3. Timer = nil;

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:

[CPP]View Plain Copy
  1. //Off timer   
  2. [MyTimer setfiredate:[nsdate distantfuture];

then you can use the following method to open this timer again:

[CSharp]View Plain Copy
  1. //Turn on timer   
  2. [MyTimer setfiredate:[nsdate Distantpast];

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:

[CPP]View Plain Copy
  1. //page will enter the foreground, turn on the timer   
  2. -(void ) Viewwillappear: ( bool ) animated  
  3. {
  4. //Turn on timer   
  5. [Scrollview.mytimer setfiredate:[nsdate Distantpast];
  6. }
  7. //page disappears, go backstage do not show this page, turn off timer   
  8. -(void ) Viewdiddisappear: ( bool ) animated  
  9. {
  10. //Off timer   
  11. [Scrollview.mytimer setfiredate:[nsdate distantfuture];
  12. }

Timer Nstimer in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.