為什么一個(gè)無(wú)窮循環(huán),一個(gè)只運(yùn)行一次?
為什么
當(dāng)按鈕start被點(diǎn)擊時(shí),setTimeout()調(diào)用函數(shù),在5秒后彈出一個(gè)提示框?? 只運(yùn)行一次?
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function tinfo(){
?var t=setTimeout("alert('Hello!')",5000);
}
</script>
</head>
<body>
<form>
?<input type="button" value="start" onClick="tinfo()">
</form>
</body>
</html>
而這個(gè)代碼就創(chuàng)建一個(gè)運(yùn)行于無(wú)窮循環(huán)中的計(jì)數(shù)器,而不是只運(yùn)行一次?
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var num=0;
function numCount(){
document.getElementById('txt').value=num;
num=num+1;
setTimeout("numCount()",1000);
}
</script>
</head>
<body>
<form>
<input type="text" id="txt" />
<input type="button" value="Start" onClick="numCount()" />
</form>
</body>
</html>
2016-04-13
下面這個(gè)函數(shù),在函數(shù)里面調(diào)用了自己,相當(dāng)于運(yùn)行setTimeout("numCount()",1000);的時(shí)候,又將nunCount()重新運(yùn)行了一次……