我有一个从网络获取数据的功能。但是当这个功能执行时,需要一段时间。因此,我添加了一个停止按钮。但是,当用户点击停止按钮时,如何终止功能?
终止的功能:
func getData(param: String) -> String{
//param can be one of these = row, name, channel, rating, share
var xPath: String = ""
if param == "row" {
xPath = "/html/body/table[4]/tr[4]/td[@class='sayfa_ic']/table/tr[2]/td/table//tr/td[1]/b"
} else if param == "name" {
xPath = "/html/body/table[4]/tr[4]/td[@class='sayfa_ic']/table/tr[2]/td/table//tr/td[2]"
} else if param == "channel" {
xPath = "/html/body/table[4]/tr[4]/td[@class='sayfa_ic']/table/tr[2]/td/table//tr/td[3]"
} else if param == "rating" {
xPath = "/html/body/table[4]/tr[4]/td[@class='sayfa_ic']/table/tr[2]/td/table//tr/td[4]"
} else if param == "share" {
xPath = "/html/body/table[4]/tr[4]/td[@class='sayfa_ic']/table/tr[2]/td/table//tr/td[6]"
} else {
return ""
}
let html = Ji(htmlURL: NSURL(string: URL!)!)//takes a while
let titleNode = html!.xPath(xPath)!
return String(titleNode)
}
答案 0 :(得分:0)
如果您使用的是NSURLSessionTask,请致电<html>
<body>
<form name="mShow">
<input type="text" name="MX" value="0" size="4"> X
<br>
<input type="text" name="MY" value="0" size="4"> Y
<br>
</form>
<script language="JavaScript1.2">
var IE = document.all ? true : false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouse;
var tempX = 0
var tempY = 0
function getMouse(e) {
if (IE) {
tempX = event.clientX + document.body.scrollLeft
tempY = event.clientY + document.body.scrollTop
} else {
tempX = e.pageX
tempY = e.pageY
}
if (tempX < 0) {
tempX = 0
}
if (tempY < 0) {
tempY = 0
}
// ADD TEMP VALUE FOR FIELDS
document.mShow.MX.value = tempX
document.mShow.MY.value = tempY
return true
}
</script>
</body>
</html>
。
文档:
task.cancel()
使用Synchronous API会很困难;您需要一个异步API,并且调用cancel将只会产生错误,因为无论如何可能由于任何原因而发生错误,您应该正确处理它。