作者No (you stay there)
看板Ajax
標題Re: [ js ] setTimeout更新頁面
時間Tue Oct 16 04:22:21 2012
※ 引述《LordCHTsai (我餓了)》之銘言:
: 正在寫一個投票系統,投票到開票時間設定之後,介面會根據時間自動轉換
: js的部分
: function checkstatus()
: {
: var xmlhttp;
: if(window.XMLHttpRequest)
: {
: xmlhttp = new XMLHttpRequest();
: }
: else
: {
: xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
: }
你這是用同步寫法
應該呼叫send(),server回應之後才進這段
但這個順序導致恆false
要驗證就加個alert或者console.log()
把xmlhttp.readyState亮出來看看
: 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;
: }
: }
: }
上面是同步的寫法
但這裡非同步AJAX
↓這裡
: xmlhttp.open("GET","check_status.php",true);
: xmlhttp.send();
: setTimeout(function(){checkstatus();},3000);
: }
: 然後html用<script>check_status()</script>來呼叫
: 可是每次都執行一次就停止了
: 而且我在function內放alert也沒有反應,完全不知道程式跑到哪
: 請求一下協助
所以問題不是在setTimeout
而是在同步非同步AJAX混在一起了
所以就看你是要做同步還是非同步來改吧~
--
▄ 鬼打牆.c - Code::Blocks http://鬼打牆.com/ __
438● if(!mark[i][j] && (0 != (nums[i] & mask[j]))){ ︿
439 ma Warning!!! ○ │
440 vi 程式寫到鬼打牆?快上 http://ghosthitswall.com/ ag);
441 ma 是 好 │
442 } //ψhtx9 ↗ ﹀
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.165.185.214