0

xmlhttp 请求每次都返回不同的数值。我想每 2 秒调用一次,最多 10 次,并将这些值发送到数组中。

我尝试了以下方法。它不起作用

function dif() {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
var returned = (new Date).getTime();
if (request.readyState === 4 && request.status === 200) {
    var timestamp = request.responseText.split('|');
    var original = + timestamp[0];
    var receive = + timestamp[1];
    var timeoffset = original - receive;
    return timeoffset

}
}
request.open("POST", "sync.php", true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("original=" + (new Date).getTime());

}


var times = [];

function time() {
times.push(dif());
if (times.length < 10) {
    setTimeout(time, 2000);
}
}

setTimeout(time, 100);

请告诉我如何将这些值放入数组 times=[]

4

0 回答 0