我有一个setTimeout函数,如下所示。
setTimeout(function(){gameEngine(radioValue,height,width,gameLevel)}, 1500);
我使用了以下clearTimeout格式,但那不起作用。怎么了?
clearTimeout(gameEngine(radioValue,height,width,gameLevel));
答案 0 :(得分:0)
setTimeout
函数返回一个ID,您可以将其传递给clearTimeout
函数以停止计时器。
let gameTimer = setTimeout(function(){...}, 1500);
现在,无论何时您想停止计时器,都调用clearTimeOut
方法并将其传递给您的计时器ID。
clearTimeout(gameTimer);
答案 1 :(得分:0)
您需要将@IBAction func usdConversions(_ sender: Any) {
self.displayAlert("Alert!", message: "USD Selected")
let usdVal = (outputCurrency1.text! as NSString).floatValue
let euroValue = usdVal * (usdtoeurquote["5. Exchange Rate"] as! Float)
outputCurrency2.text = String(format: "%.2f", euroValue)
let gbpVal = usdVal * (usdtogbpquote["5. Exchange Rate"] as! Float)
outputCurrency3.text = String(format: "%.2f", gbpVal)
let cnyVal = usdVal * (usdtocnyquote["5. Exchange Rate"] as! Float)
outputCurrency2.text = String(format: "%.2f", cnyVal)
let cadVal = usdVal * (usdtocadquote["5. Exchange Rate"] as! Float)
outputCurrency2.text = String(format: "%.2f", cadVal)
let inrVal = usdVal * (usdtoinrquote["5. Exchange Rate"] as! Float)
outputCurrency2.text = String(format: "%.2f", inrVal)
let sekVal = usdVal * (usdtosekquote["5. Exchange Rate"] as! Float)
outputCurrency2.text = String(format: "%.2f", sekVal)
let rubVal = usdVal * (usdtorubquote["5. Exchange Rate"] as! Float)
outputCurrency2.text = String(format: "%.2f", rubVal)
let nzdVal = usdVal * (usdtonzdquote["5. Exchange Rate"] as! Float)
outputCurrency2.text = String(format: "%.2f", nzdVal)
}
函数存储在变量中(例如x)
当您想setTimeOut
时,使用参数x进行调用。
例如:
cleartimeout
var timeoutID;
function delayedAlert() {
timeoutID = window.setTimeout(slowAlert, 10000);
}
function slowAlert() {
alert("That was really slow!");
}
function clearAlert() {
window.clearTimeout(timeoutID);
}
根据您的情况:
<p>Live Example</p>
<button onclick="delayedAlert();">Show an alert box after 10 seconds</button>
<p></p>
<button onclick="clearAlert();">Cancel alert before it happens</button>