Use of Timer Nstimer in iOS
1. Initialization
+ (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.0 target: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:[Nsthread Sleepfortimeinterval:5.0];//pause 5s.//Use of timer:Nstimer *connectiontimer;//Timer Object//Instantiating a timerSelf.connectiontimer=[nstimer Scheduledtimerwithtimeinterval:1.5target:self selector: @selector (timerfired:) Userinfo:nil Repeats:no]; [[Nsrunloop Currentrunloop]addtimer:self.connectiontimer Formode:nsdefaultrunloopmode];//a method of using timer as delay Do{[[Nsrunloopcurrentrunloop]rununtildate:[nsdatedatewithtimeintervalsincenow:1.0]];} while(!Done ); //Timer Call Function-(void) timerfired: (Nstimer *) Timer{done=YES;}
Use of Timer Nstimer in iOS