我正在尝试创建一个网页,当它开始加载时,使用Interval来启动计时器.
页面完全加载后,它会停止计时器,
但99%的时间我得到0.00或0.01的时间测量,即使它需要更长时间.
偶尔,它会说某些更有意义的东西,比如.28或3.10.
这是代码,如果它有帮助:
var hundredthstimer = 0;
var secondplace = 0;
function addinc(){
hundredthstimer += 1;
if (inctimer == 100){
hundredthstimer = 0;
secondplace += 1;
}
}
var clockint = setInterval(addinc, 10);
function init(){
var bconv1 = document.getElementById("bconverter1");
var bconv2 = document.getElementById("bconverter2");
$(bconv2).hide();
clearInterval(clockint);
if (inctimer.len !== 2){
inctimer = "0" + inctimer;
}
alert(secondplace + "." + inctimer);
}
onload = init;
Run Code Online (Sandbox Code Playgroud)
所以它基本上创建了一个名为百分时的变量,每10毫秒(.01秒)增加1.
然后,如果此数字达到1000(1整秒),则名为secondsplace的变量将增加1,因为这是它运行了多少个完整秒数.
然后,它会提醒秒位置,小数点和百分位数作为总加载时间.
但上述数字不正确的问题仍然存在.为什么?
好的,我可以使用访问HTTP ajax响应头
xhr.getAllResponseHeaders();
Run Code Online (Sandbox Code Playgroud)
但它似乎没有得到它的日期,虽然它在那里:
[Chrome]
**Response Header**
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:8092
Content-Type:application/json; charset=utf-8
**Date:Thu, 15 Jan 2015 16:30:13 GMT**
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
TotalCount:116
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
Run Code Online (Sandbox Code Playgroud)
并且代码仅显示以下内容:
[output on alert xhr.getAllResponseHeaders();]
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Run Code Online (Sandbox Code Playgroud)
这是ajax电话:
$.ajax({
url: url,
type: "GET",
contentType: "application/json;charset=utf-8",
async: true,
success: function (data,status, xhr) {
displayNewData(data);
alert(xhr.getAllResponseHeaders());
},
error: function () {
alert(url);
}
});
Run Code Online (Sandbox Code Playgroud)
有没有办法在响应标题中获取日期?