首页 > 代码库 > ajax callback 在什么时候运行
ajax callback 在什么时候运行
html代码
<!DOCTYPE html><html><head><title>Ajax Thread</title><meta charset="utf-8"/></head><body> <div>Comet return:</div> start:<div id="start"> </div> get:<div id="get"> </div> sended:<div id="sended"> </div> pause:<div id="pause"> </div> callback:<div id="callback"> </div> pause2:<div id="pause2"> </div><script type="text/javascript">if (!window.console) { var console = { log: function() {} };}var xmlhttp;function loadXMLDoc(url, callback) { xmlhttp = null; if (window.XMLHttpRequest) { // code for IE7, Firefox, Opera, etc. xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp != null) { xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { // 4 = "loaded" if (xmlhttp.status == 200) { // 200 = "OK" //dom(‘A1‘).innerHTML = xmlhttp.status; //dom(‘A2‘).innerHTML = xmlhttp.statusText; //dom(‘A3‘).innerHTML = xmlhttp.responseText; callback(xmlhttp.responseText); } else { //alert("Problem retrieving XML data:" + xmlhttp.statusText); } } } xmlhttp.open("GET", url, true); xmlhttp.send(null); } else { //alert("Your browser does not support XMLHTTP."); }}function pause(numberMillis, id) { var now = new Date(); var exitTime = now.getTime() + numberMillis; var count = 0; var lastTime = parseInt(now.getTime() / 1000); while (true) { now = new Date(); if (lastTime == parseInt(now.getTime() / 1000)) { count++; } else { dom(id).innerHTML = dom(id).innerHTML + lastTime + ‘ ‘ + count + ‘<br>‘; console.log(id, lastTime, count); lastTime = parseInt(now.getTime() / 1000); count = 0; } if (now.getTime() > exitTime) { return; } }}function dom(id) { return document.getElementById(id);}(function() { dom("start").innerHTML = parseInt(new Date().getTime() / 1000); console.log(‘start‘, parseInt(new Date().getTime() / 1000)); loadXMLDoc("/get.php?" + Math.random(), function(data) { console.log(‘callback‘, parseInt(new Date().getTime() / 1000)); dom("callback").innerHTML = dom("callback").innerHTML + ‘--我在前面是ajax是同步执行--‘; dom("callback").innerHTML = dom("callback").innerHTML + parseInt(new Date().getTime() / 1000); dom("get").innerHTML = data + ‘<br>‘; pause(2000, ‘pause2‘); }); dom("sended").innerHTML = parseInt(new Date().getTime() / 1000); dom("callback").innerHTML = dom("callback").innerHTML + ‘--我在前面是ajax是异步执行--‘; console.log(‘sended‘, parseInt(new Date().getTime() / 1000)); pause(6000, ‘pause‘);})();</script></body></html>
php代码
<?php$s = time();sleep(4);echo "i am coming! ";echo "get:".$s." send:".time();?>
输出结果
ajax callback 在什么时候运行
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。