首页 > 代码库 > 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">&nbsp;</div>  get:<div id="get">&nbsp;</div>  sended:<div id="sended">&nbsp;</div>  pause:<div id="pause">&nbsp;</div>  callback:<div id="callback">&nbsp;</div>  pause2:<div id="pause2">&nbsp;</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 在什么时候运行