[ js ] setTimeout更新頁面

看板Ajax作者 (我餓了)時間11年前 (2012/10/15 18:53), 編輯推噓0(004)
留言4則, 3人參與, 最新討論串1/2 (看更多)
正在寫一個投票系統,投票到開票時間設定之後,介面會根據時間自動轉換 js的部分 function checkstatus() { var xmlhttp; if(window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp.readyState==4 && xmlhttp.status==200) { if( document.getElementById(mode).innerHTML != xmlhttp.responseText ) { switch(xmlhttp.responseText) { case prepared: document.location.href="prepared.php"; break; case voting: document.location.href="voting.php"; break; case completed: document.location.href="completed.php"; break; } } } xmlhttp.open("GET","check_status.php",true); xmlhttp.send(); setTimeout(function(){checkstatus();},3000); } 然後html用<script>check_status()</script>來呼叫 可是每次都執行一次就停止了 而且我在function內放alert也沒有反應,完全不知道程式跑到哪 請求一下協助 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 68.33.32.85

10/15 21:06, , 1F
check_status? checkstatus?
10/15 21:06, 1F
貼錯了,因為我後來改了function名稱 <script>checkstatus()</script>才對

10/15 22:25, , 2F
setTimeout( "check_status()", 3000 )
10/15 22:25, 2F
試過這種,也一樣 ※ 編輯: LordCHTsai 來自: 68.33.32.85 (10/16 01:56)

10/16 22:16, , 3F
我只貼你的JS,可以三秒跑一次
10/16 22:16, 3F

10/16 22:17, , 4F
你不是試要執行了document.location.href,還能跑吧?
10/16 22:17, 4F
文章代碼(AID): #1GU-iS1a (Ajax)
文章代碼(AID): #1GU-iS1a (Ajax)