Take the 2-minute tour ×

How can I encode on objective-C the line of javascript:

setTimeout("function("+argument+")", value);
share|improve this question
add comment

3 Answers

It is actually not very clear what do you want. I suppose that you want to do that in Objective C way. So you need to use NSTimer:

[NSTimer scheduledTimerWithTimeInterval: value
                                 target: self
                               selector: @selector(function:)
                               userInfo: argument repeats: YES];

-(void) function:(NSTimer*) timer {
   id argument = timer.userInfo;

Edit: however if you just want to use that as string then write as Daniel has shown.

share|improve this answer
add comment

Firstly with javascript although you can use a string evaluation in "setTimeout", it's expensive and far less readable.

It is far better to use a reference instead. Please read http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/.

Lets re-write your Javascript as follows:

var myFunction = function (arguments) {

var value = 1000;

var myTimeout = setTimeout(myFunction('Hello World'), value);

The above structure translates into the NSTimer class function quite well.

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats

First define your function:

-(void) myFunction {
    // do somthing

And to call your function after some delay...

NSTimeInterval value = 1.0;    
NSTimer *myTimeout = [NSTimer scheduledTimerWithTimeInterval:value 

And there you have it. An Asynchronous function call after a delay of 1 second.

Where are the parameters you may ask? Well it is a little extra step requireing a NSInvocation object which is explained quite nicely at Arguments in @selector

By the way the question "How can I encode on objective-C the line of javascript:" is not clear, i believe you requiring an example similar to the setTimeout method Javascript but in Objective-C

share|improve this answer
add comment
@"setTimeout(\"function(\"+argument+\")\", value);"
share|improve this answer
add comment

Your Answer


