iOS:计时器(5秒执行一次)

iOS 计时器(5秒执行一次)


每5秒执行一次,切记离开页面时取下进程。

/** 获取一个全局的线程来运行计时器*/
    dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    /** 创建一个计时器*/
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue2);
    /** 设置计时器, 这里是每5秒执行一次*/
    dispatch_source_set_timer(_timer, dispatch_walltime(nil, 0), 5*1000*NSEC_PER_MSEC, 0);
    /** 设置计时器的里操作事件*/
    dispatch_source_set_event_handler(_timer, ^{
       //do you want....
    });
    dispatch_resume(_timer);
/*取消计时器*/
dispatch_source_cancel(_timer);

猜你喜欢

转载自blog.csdn.net/qq_15289761/article/details/106825443