Objective-C 上的 setTimeout("function (" "+ argument "#x2B;")", value)

发布于 2024-11-08 12:17:27 字数 98 浏览 3 评论 0 原文

我如何在 Objective-C 上编码 javascript 行:

setTimeout("function("+argument+")", value);

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

setTimeout("function("+argument+")", value);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩起发的微风 2024-11-15 12:17:28

其实不是很清楚自己想要什么。我想你想以 Objective C 的方式做到这一点。因此,您需要使用 NSTimer:

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

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

编辑:但是,如果您只想将其用作字符串,请按照丹尼尔所示进行编写。

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.

深居我梦 2024-11-15 12:17:28

首先,尽管您可以在“setTimeout”中使用字符串计算,但它的成本昂贵且可读性差得多。

最好使用参考。请阅读 http://yuiblog.com/blog /2006/11/13/javascript-we-hardly-new-ya/

让我们按如下方式重写您的 Javascript:

var myFunction = function (arguments) {
    alert(arguments);
};

var value = 1000;

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

上面的结构可以很好地转换为 NSTimer 类函数。

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

首先定义你的函数:

-(void) myFunction {
    // do somthing
}

并在延迟一段时间后调用你的函数......

NSTimeInterval value = 1.0;    
NSTimer *myTimeout = [NSTimer scheduledTimerWithTimeInterval:value 
    target:self 
    selector:@selector(myFunction) 
    userInfo:nil 
    repeats:NO];

就这样。延迟 1 秒后异步函数调用。

您可能会问参数在哪里?好吧,这是一个需要 NSInitation 对象的额外步骤,在 @selector 中的参数

对此 进行了很好的解释问题“我如何在 Objective-C 上编码 javascript 行:”的方式尚不清楚,我相信您需要一个类似于 setTimeout 方法 Javascript 但在 Objective-C 中的示例

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) {
    alert(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 
    target:self 
    selector:@selector(myFunction) 
    userInfo:nil 
    repeats:NO];

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

淡笑忘祈一世凡恋 2024-11-15 12:17:28
@"setTimeout(\"function(\"+argument+\")\", value);"
@"setTimeout(\"function(\"+argument+\")\", value);"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文