Re: [ js ] setTimeout更新頁面

看板Ajax作者 (you stay there)時間13年前 (2012/10/16 04:22), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《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
文章代碼(AID): #1GV727ZD (Ajax)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
文章代碼(AID): #1GV727ZD (Ajax)