我有一个功能。它运行得很好,但是当我想再次调用它时,它知道该函数已经完成,所以如果我再次调用它,它将自动传递给已完成。如何重置完成处理程序?
代码:
func Test(completionHandler: @escaping (_ finished: String?) -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
completionHandler("finished") // tell the caller that this has completed
}
}
我称之为:
self.Test(completionHandler: { finished in
print("We have completed function)")
})
如果我在大约一分钟后再打电话。而不是像它假设的那样等待10秒。它会自动打印出来。
print("We have completed function)")
要了解的事项:
上面的代码是一个测试示例,我真正的代码就是这个。
func GoToPage(completionHandler: @escaping (_ finished: String?) -> Void) {
self.webView.evaluateJavaScript("document.getElementById('results').getElementsByClassName('page')[0].getElementsByClassName('name')[0].innerText"){ (value, error) in
if error != nil {
} else {
print(value)
completionHandler("finished")
}
}
}
我称之为:
self.GoToPage(completionHandler: { finished in
print("We have completed function)")
})
然后我等了1-2分钟,然后以同样的方式再次打电话。
DispatchQueue.main.asyncAfter(deadline: .now() + 130.0) {
self.GoToPage(completionHandler: { finished in
print("We have completed function")
})
}
它给我打印了一条线,我们已经完成了所谓的功能。
示例:
这就把它放在一起
self.GoToPage(completionHandler: { finished in
print("We have completed function)")
NextStep()
})
func NextStep(){
DispatchQueue.main.asyncAfter(deadline: .now() + 130.0) {
self.GoToPage(completionHandler: { finished in
print("We have completed function")
})
}
}
func GoToPage(completionHandler: @escaping (_ finished: String?) -> Void) {
self.webView.evaluateJavaScript("document.getElementById('results').getElementsByClassName('page')[0].getElementsByClassName('name')[0].innerText"){ (value, error) in
if error != nil {
} else {
print(value)
completionHandler("finished")
}
}
}