通过JS算的吧。你看下
[JavaScript] 纯文本查看 复制代码 <script type="text/javascript">
function Ajax (url,params,callback) {
var xhr = !!0;
try {//IE7+,Chrome and etc.
xhr = new XMLHttpRequest;
} catch(e) {//IE6系列
try {
xhr = new ActiveXObject("MSXML2.XMLHTTP");
} catch(e2) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e3) {
xhr = false;
}
}
}
if (!xhr)return;
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && callback) {
callback(xhr.status == 200 ? xhr : null);
}
};
xhr.send(params);
}
function loginForm (argument) {
var userName = document.getElementById("user_name"),tN;
while( !tN || tN > 900){
tN = tN = parseInt((+(new Date())-(Math.random())*10).toFixed(3).slice(-3).replace('.',''),10);
}
userName.value = 't' + parseInt(tN,10);
document.getElementById("login_form").submit();
}
var li = document.getElementsByTagName("li"),
st, count = 0,
//btn = document.getElementsByTagName('button')[0],
speed = document.getElementById('speed');
var cache = [],
timeout = null;
var cacheImg = function() {
var cid = '';
for (var c = 0; c < li.length; c++) {
var clink = {}
cid = c + '';
clink.img = document.createElement('img');
clink.url = li[c].getElementsByTagName("a")[0].href.split('/').slice(0, 3).join('/') + "/speed.gif?";
clink.time = 0;
clink.number = 0;
clink.img.onerror = getError;
clink.img.onload = getLoad;
clink.img.id = cid;
clink.loadTime = [];
clink.stop = false;
cache[c] = clink;
}
}
var clear = function(c) {
clearTimeout(timeout);
timeout = null;
if (!c) {
for (var i = li.length - 1; i >= 0; i--) {
if (li[i].getElementsByTagName('span').length) {
li[i].getElementsByTagName('span')[0].innerHTML = '';
}
};
};
}
var test = function(c) {
clear(c);
//btn.setAttribute('disabled', 'disabled');
var cid = "";
if (cache.length > 0 && cache[c]) {
if (li[c].getElementsByTagName("span").length > 0) {
li[c].getElementsByTagName("span")[0].innerHTML = "測速中";
cache[c].img.src = cache[c].url + (Math.random() + '').replace('0.', '');
} else {
var span = document.createElement("span");
span.id = c + "m";
span.innerHTML = "測速中";
li[c].appendChild(span);
cache[c].img.src = cache[c].url + (Math.random() + '').replace('0.', '');
speed.appendChild(cache[c].img);
}
cache[c].time = new Date().getTime();
cache[c].stop = false;
timeout = setTimeout(function() {
getError.call(cache[c].img, c);
}, 5000);
} else {
//btn.removeAttribute('disabled');
};
}
function getError(c) {
var n = this.id;
if (typeof c === 'number') {
n = c;
};
n = parseInt(n, 10);
if (document.getElementById(this.id + 'm')) {
if (!cache[parseInt(n, 10)].stop) {
cache[parseInt(n, 10)].number = 0;
cache[parseInt(n, 10)].stop = true;
if (document.getElementById(n + 'm').innerHTML != '超時' || document.getElementById(n + 'm').innerHTML == '測速中') {
document.getElementById(n + 'm').innerHTML = '無法鏈接';
}
};
setTimeout(function() {
test(n + 1);
}, 500);
}
}
function getLoad() {
var end = new Date().getTime(),
cid = parseInt(this.id, 10),
clink = cache[cid],
td = clink.number ? end - cache[cid].time : end - cache[cid].time,
total = 0;
cache[cid].loadTime.push(td);
cache[cid].number += 1;
if (cache[cid].stop) {
return;
}
if (cache[cid].number < 2) {
setTimeout(function() {
test(cid);
}, 500);
} else {
for (var c = 0; c < 2; c++) {
total += clink.loadTime[c];
}
document.getElementById(cid + 'm').innerHTML = (total / 2).toFixed(2) + '毫秒';
count -= 1;
cache[cid].number = 0;
cache[cid].loadTime.length = 0;
cache[cid].stop = true;
setTimeout(function() {
test(cid + 1);
}, 500);
}
}
cacheImg();
</script>
|