我对javascript比较新手。我写了开始计数到10时,直到它达到1如何将参数传递给setTimeout函数
<script type="text/javascript">
function countDown(secs) {
var element = document.getElementById("status");
element.innerHTML = "Please wait for "+secs+" seconds";
if(secs < 1) {
clearTimeout(timer);
element.innerHTML = '<h2>Countdown Complete!</h2>';
element.innerHTML += '<a href="#">Click here now</a>';
}
secs--;
---> **var timer = setTimeout('countDown('secs')',1000);**
}
</script>
<div id="status"></div>
<script type="text/javascript">countDown(10);</script>
然后我试图传递参数作为'+secs+'
到倒计时功能的简单的计数器程序。
var timer = setTimeout('countDown('+secs+')',1000);
以上更改的作品。
我的问题是为什么我需要传递参数为'+ secs +'而不是'secs'? 它有什么不同?
[这里](HTTP: //stackoverflow.com/a/10313023/1144176)'s精彩概述如何正确使用'setTimeout'。 –
@JakeStoeffler真棒链接! – rab
可能重复的[JavaScript,setTimeout](http://stackoverflow.com/questions/10312963/javascript-settimeout) – jahroy