我刚刚在Firefox上遇到了setTimeout()
的问题,如果每个人都有这个问题似乎会是个大问题,但是当我搜索它时我没有提到它...所以我想知道如果我以某种方式把它搞砸了。
问题由this W3Schools TryIt page非常清楚地说明。对于后代,代码如下:
<!DOCTYPE html>
<html>
<body>
<p>Click the Start button to output "Hello" after 2 seconds.</p>
<p>In this example, we also output the parameters that were passed to the alertFunc() function (does not work in IE9 and earlier).</p>
<button onclick="myStartFunction()">Start</button>
<p id="demo"></p>
<p id="demo2" style="color:red;"></p>
<script>
var myVar;
function myStartFunction() {
myVar = setTimeout(alertFunc, 2000, "First parameter", "Second parameter");
}
function alertFunc(param1, param2) {
document.getElementById("demo").innerHTML += "Hello ";
document.getElementById("demo2").innerHTML = "Parameters passed to alertFunc(): <br>"
+ param1 + "<br>" + param2 + "<br>";
}
</script>
</body>
</html>
当我在Chrome中执行此操作时(目前为Version 53.0.2785.101 m (64-bit)
,它可以正常工作。当我在Firefox中执行此操作时(当前为48.0.2
),它会失败(两个参数值都显示为undefined
)