Four easy-to-use demos, about iOS timers and latency, are very handy.

Source: Internet
Author: User

1, delayed execution (non-repeatable)

[OBJC] View plain copy /* *  * * Delay cannot be duplicated  *  /-(void) timermethoda  {      [self Performselector: @selector (methodaevent)                 withobject:nil                 afterdelay:2.0f]; // Delay Time   }    -(void) methodaevent  {      NSLog (@ "--method_a ");  }  


Effect I directly intercept the console log, do not do the UI.

2, use Nstimer to perform timing and delay (repeatable)

[OBJC] View plain copy/** * * Timer can be repeated **/- (void) Timermethodb {_timer= [Nstimer scheduledtimerwithtimeinterval:1.0f  //time intervaltarget:self selector: @selec                                               Tor (methodbevnet) Userinfo:nil  Repeats:yes]; }    - (void) methodbevnet {count++; NSLog (@"--method_b count:%d", Count); if(Count >=5) {[_timer invalidate]; //Repeat 5 times, timer stopsNSLog (@"--End"); }  } 

3, perform timing with dispatch_source_set_timer (repeatable)

[OBJC] View plain copy/** * * * Dispatch_time can be repeated **/- (void) timermethodc {__blockinti =0; CGFloat Duration=1.0f;//time intervaldispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); dispatch_source_t Timer= Dispatch_source_create (Dispatch_source_type_timer,0,0, queue); Dispatch_source_set_timer (Timer, Dispatch_time_now, duration* Nsec_per_sec,0); Dispatch_source_set_event_handler (Timer,^{i++; if(I >5) {dispatch_source_cancel (timer); //stop after 5 executionsNSLog (@"--End"); }Else{NSLog (@"--Method_c i:%d", i);      }      });  Dispatch_resume (timer); }  

4, execution delay with dispatch_after (non-repeatable)

[OBJC] View plain copy /* *  * * * dispatch_time not repeatable  **  /-(void) timermethodd  {        2.0f;   // delay Time      dispatch_time_t timer = Dispatch_time (Dispatch_time_now, (int64_t) (Delaytime *nsec_per_sec));       ^{          NSLog (@ "--method_d time:%@", [nsdate Date]);      }  

The article concludes with a demo

http://download.csdn.net/detail/xiongbaoxr/9417374

Four easy-to-use demos, about iOS timers and latency, are very handy.

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.