Opening and closing of timer Nstimer in IOS object-c

Source: Internet
Author: User

Call the Timer method once:

The code is as follows Copy Code

MyTimer = [Nstimer scheduledtimerwithtimeinterval:1.5 target:self selector: @selector (Scrolltimer) Userinfo:nil Repeats:no];
Do not repeat, call only once. The timer will automatically stop running once it is run

To call the Timer method repeatedly:

The code is as follows Copy Code

Timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (function:) Userinfo:nil repeats: YES];
The function method is run once every 1 seconds.

Note: When you set the counter's repeats to Yes, the self's reference count is added by 1. As a result, self (that is, Viewcontroller) may not release, so you must stop the counter timer when viewwillappear, or it may cause a memory leak.

Stop the timer from running, but this is a permanent stop: (note: After stopping, be sure to empty the timer, otherwise there is no release.) Don't believe it? Try it on your own ~)

The code is as follows Copy Code
Cancel Timer
[Timer invalidate];
Timer = nil;

To do this: stop first and then run the timer again in some case, you can use the following method:

First off the timer can not use the above method, you should use the following method:

The code is as follows Copy Code

Turn off timer
[MyTimer setfiredate:[nsdate Distantfuture]];
You can then turn on this timer by using the following method:

Open Timer
[MyTimer setfiredate:[nsdate Distantpast]];
Example: For example, turn off the timer when the page disappears, and then turn on the timer when the page opens again.

(mainly to prevent it running in the background, take up the CPU) can be implemented using the following code:

The page will go into the foreground and turn on the timer
-(void) Viewwillappear: (BOOL) animated
{
Open Timer
[Scrollview.mytimer setfiredate:[nsdate Distantpast]];
}

The page disappears, goes backstage does not display the page, closes the timer
-(void) Viewdiddisappear: (BOOL) animated
{
Turn off timer
[Scrollview.mytimer setfiredate:[nsdate Distantfuture]];
}

OK, it's done.

Add:


Because you want to use the Nstimer timer to do the child thread loop execution task. Now briefly explain how to use the Nstimer.

The code is as follows Copy Code
Nstimer *mytimer = [Nstimer scheduledtimerwithtimeinterval:1.5 target:self selector: @selector (scrolltimer) UserInfo: Nil Repeats:no];

Call once per 1.5s Scrolltimer only once

Repeats:no to single loop Yes to repeat loop

Permanent Stop Timer

The code is as follows Copy Code
[Timer invalidate];

To do this: stop first and then run the timer again in some case, you can use the following method:

First off the timer can not use the above method, you should use the following method:

  code is as follows copy code


//Close Timer
[MyTimer setfiredate:[nsdate Distantfuture]];
You can then turn on this timer by using the following method:


//Open Timer
[MyTimer setfiredate:[nsdate Distantpast]];
Example: For example, turn off the timer when the page disappears, and then turn on the timer when the page opens again. The
(primarily to prevent it from running in the background, take up the CPU) can be implemented using the following code:


//page will go to the foreground, turn on the timer
-(void) Viewwillappear: (BOOL) animated
{
//Open timer
[Scrollview.mytimer Setfiredate:[nsdate Distantpast]];
}

//page disappears, go backstage do not display the page, turn off timer
-(void) Viewdiddisappear: (BOOL) animated
{
//close Timer
[ Scrollview.mytimer setfiredate:[nsdate Distantfuture]];
}

If the timer was opened on a child thread
You need to add a timer to the runloop of the thread. Otherwise the timer will fail.

The code is as follows Copy Code

[[Nsrunloop Currentrunloop]addtimer:mytimer Formode:nsdefaultrunloopmode];

Related Article

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.