Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C equivalent to javascripts setTimeout?

I was wondering whether there is a solution to raise an event once after 30 seconds or every 30 seconds in CocoaTouch ObjectiveC.

like image 842
jantimon Avatar asked Sep 16 '09 09:09

jantimon


1 Answers

The performSelector: family has its limitations. Here is the closest setTimeout equivalent:

dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 0.5); dispatch_after(delay, dispatch_get_main_queue(), ^(void){     // do work in the UI thread here }); 

EDIT: A couple of projects that provide syntactic sugar and the ability to cancel execution (clearTimeout):

  • https://github.com/Spaceman-Labs/Dispatch-Cancel
  • https://gist.github.com/zwaldowski/955123
like image 58
Blago Avatar answered Sep 22 '22 03:09

Blago