利用GCD做定时器

2016-05-07  本文已影响24人  Mr_Zeng

NSInteger noc=0;

NSTimeInterval period = 60.0; //设置时间间隔

dispatch_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_walltime(NULL, 0), period * NSEC_PER_SEC, 0);

//每秒执行

dispatch_source_set_event_handler(_timer, ^{

if (noc==1) { dispatch_source_cancel(_timer);}//取消定时

//在这里执行事件

[self getNowDate];

});

dispatch_resume(_timer);

上一篇 下一篇

猜你喜欢

热点阅读