得到这个脚本:
<script type="text/javascript">
var imones = [];
imones.push('example.com');
imones.push('cnn.com');
imones.push('apple.com');
var beforeloadingtime = (new Date()).getTime();
var beforeTime = [];
beforeTime[0] = beforeloadingtime;
$.each(imones, function (index, value) {
var jsonnuorodos = "http://anyorigin.com/get?url=" + imones[index] + "&callback=?";
$.getJSON(jsonnuorodos, function (data) {
var iframe = $("#output")[0];
var doc = iframe.document;
if (iframe.contentDocument) {
doc = iframe.contentDocument;
} else if (iframe.contentWindow) {
doc = iframe.contentWindow.document;
}
doc.open();
doc.writeln(data.contents);
doc.close();
var loadingtime = (new Date()).getTime();
beforeTime.push(loadingtime);
var result;
result = (beforeTime[index + 1] - beforeTime[index]) / 1000;
console.log("result - " + result);
});
});
</script>
在控制台中我得到这些结果:
result - 1.741
result - NaN
result - 0.86
我在第二个结果中得到 NaN,因为 beforeTime[index+1] 值始终无法识别。
编辑:如果我写console.log("index= "+[index]); console.log("index+1= "+[index+1]);
我得到:
index= 0
index+1= 1
index= 2
index+1= 3
index= 1
index+1= 2
它应该在哪里:
index= 0
index+1= 1
index= 1
index+1= 2
index= 2
index+1= 3