Enable and disable Timer Nstimer in iOS

Source: Internet
Author: User

Call the Timer method once:

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    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 Plaincopy
    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 Plaincopy
    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 Plaincopy
    1. Turn off the timer
    2. [MyTimer setfiredate:[nsdate distantfuture];


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

[CSharp]View Plaincopy
    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 Plaincopy
  1. The 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. The page disappears, goes into the background does not display this page, closes the timer
  8. -(void) Viewdiddisappear: (BOOL) animated
  9. {
  10. //Off timer
  11. [Scrollview.mytimer setfiredate:[nsdate distantfuture];
  12. }


OK, Fix it.

Enable and disable 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.