我正在尝试在文本输入字段上设置超时,该字段仅在用户停止输入后的第二秒内实现内部代码.因此,当用户键入时,我会不断调用cleartimeout并重新启动setTimeout.
我最初在Objective C中查看performSelector函数,但看起来没有Swift等价物.
然后我转到Swift中的GCD函数,寻找执行此操作的方法.
这是我想出的:
var delta: Int64 = 1 * Int64(NSEC_PER_SEC)
var time = dispatch_time(DISPATCH_TIME_NOW, delta)
dispatch_suspend(dispatch_get_main_queue())
dispatch_after(time, dispatch_get_main_queue(), {
println("timeout")
});
Run Code Online (Sandbox Code Playgroud)
dispatch_suspend函数不能像我希望的那样工作.
也许这里的派遣功能不合适?