[問題] Javascript從函式裡使用setTimeout
我要做一個網頁程式,網頁在一載入時就會有四個數字一直在跑,
按一下submit停止,再按一下繼續跑。
程式碼如下:
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<table>
<tr>
<td></td><td></td><td></td><td></td>
</tr>
</table>
<form method="post" action="javascript: loopit(stop++)">
<input type="submit">
</form>
<script language=javascript>
var stop=0;
loopit(stop);
function loopit(x) {
if (x%2==0) {
GiveMeNumber();
setTimeout("loopit(x);",10);
}
}
function GiveMeNumber() {
var tableCells = document.getElementsByTagName("TD");
for (j = 0; j < 4; j++) {
var randomnumber=Math.floor(Math.random()*10);
tableCells[j].childNodes[0].nodeValue = randomnumber;
}
}
</script>
</html>
上面的程式碼載入之後,setTimeout會失效,應該說不會發揮效用,
可能是問題癥結。
我猜是setTimeout裡的函式無法引用loopit函式的x變數,是這樣嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.62.220.74
※ 編輯: Zingo 來自: 61.62.220.74 (09/14 09:46)
→
09/14 10:53, , 1F
09/14 10:53, 1F
→
09/14 11:44, , 2F
09/14 11:44, 2F
→
09/14 13:25, , 3F
09/14 13:25, 3F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章