IOS Timer usage nstimer------5.20

Source: Internet
Author: User

1. Initialize
+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;
+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo: (ID) UserInfo repeats: (BOOL) Yesorno;
Note: Without scheduled initialization, manual Addtimer:formode is required: Add the timer to a runloop.
the scheduled initialization method will be added directly to the current runloop with the default mode.

Example:
Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:10.0 target:self selector: @selector (timerfired:) Userinfo:nil Repeats:no];
or
Nstimer *mytimer = [Nstimer timerwithtimeinterval:3.0target:selfselector: @selector (timerfired:) UserInfo: Nilrepeats:no];
[[Nsrunloop Currentrunloop]addtimer:mytimerformode:nsdefaultrunloopmode];

2. Trigger (START)
when the timer is created (not scheduled, added to Runloop, the timer is automatically triggered after the timeinterval seconds specified at initialization.

You can use the-(void) Fire method to trigger the timer immediately;
Note: You can use the This method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it's automatically invalidated after firing, even if its scheduled fire date have not Arriv Ed.
the timer is triggered immediately after calling this method in a repeating timer, but does not interrupt its previous execution plan;
calling this method in a timer that does not execute repeatedly will invalidate the timer immediately after it is triggered.

3. Stop
-(void) invalidate;
This is the only way to move the timer out of the Runloop.

Note:
the Nstimer can be accurate to 50-100 milliseconds.
The Nstimer is not absolutely accurate, and the intermediate time-consuming or blocking misses the next point, then the next point passes.

delay function and use of timer

//Delay function:
[nsthread sleepfortimeinterval:5.0];//pause 5s.

use of//timer:
Nstimer *connectiontimer;//timer Object

//Instantiation Timer
self.connectiontimer=[nstimer scheduledtimerwithtimeinterval:1.5target:self selector: @selector (timerfired:) Userinfo:nil Repeats:no];
[[Nsrunloop Currentrunloop]addtimer:self.connectiontimer Formode:nsdefaultrunloopmode];
//Using a timer as a method of delay
do{
[[nsrunloopcurrentrunloop]rununtildate:[nsdatedatewithtimeintervalsincenow:1.0]];
}while (!done);

//timer calling Function
-(void) timerfired: (Nstimer *) timer{
Done =yes;
}
//The code below is simple and valuable MyTimer = [Nstimer scheduledtimerwithtimeinterval:1.5 target:self selector: @selector (Scrolltimer)    Userinfo:nil Repeats:no]; Do not repeat, call only once. Timer runs once and stops automatically

To invoke the timer method repeatedly:
timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (function:) userinfo:nil Repeats:yes];
//Run the function method once every 1 seconds.

Stop the timer from running, but this is the permanent stop:
//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:
//Off timer
[MyTimer setfiredate:[nsdate distantfuture];

then you can use the following method to open this timer again://Turn on the timer [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:
//page will enter the foreground, turn on the timer
-(void) Viewwillappear: (BOOL) Animated

//Turn on timer
[Scrollview.mytimer setfiredate:[nsdate distantpast];


//page disappears, go backstage do not show this page, turn off timer
-(void) Viewdiddisappear: (BOOL) Animated

//Off timer
[Scrollview.mytimer setfiredate:[nsdate distantfuture];

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.