Html WebTiming API中的错误值
我使用的是新版本中指定的新版本,通过和IE 9 出于某种原因,我将Html WebTiming API中的错误值,html,performance,google-chrome,w3c,navigation-timing-api,Html,Performance,Google Chrome,W3c,Navigation Timing Api,我使用的是新版本中指定的新版本,通过和IE 9 出于某种原因,我将loadEventEnd设置为0。这限制了我计算实际加载时间 下面是输出和代码 connectStart is: 1308411426685 responseStart is: 1308411429541 domLoading is: 1308411429548 connectEnd is: 1308411426685 domInteractive is: 1308411430023 fetchStart is: 13084114
loadEventEnd
设置为0
。这限制了我计算实际加载时间
下面是输出和代码
connectStart is: 1308411426685
responseStart is: 1308411429541
domLoading is: 1308411429548
connectEnd is: 1308411426685
domInteractive is: 1308411430023
fetchStart is: 1308411426667
secureConnectionStart is: 0
domainLookupStart is: 1308411426667
responseEnd is: 1308411429543
requestStart is: 1308411426685
loadEventEnd is: 0
domComplete is: 0
redirectStart is: 0
unloadEventEnd is: 1308411429545
domContentLoadedEventStart is: 1308411430023
domContentLoadedEventEnd is: 0
domainLookupEnd is: 1308411426667
navigationStart is: 1308411426667
unloadEventStart is: 1308411429545
loadEventStart is: 0
redirectEnd is: 0
代码:
var performance=window.performance | | window.mozPerformance | | | window.msPerformance | | | |{};
var timing=performance.timing | |{};
函数getTiming(计时str){
如果(!计时)返回0;
var时间=定时[定时STR];
返回时间;
}
var loadTime=(new Date().getTime()-getTiming(“navigationStart”))/1000;
$(文档).ready(函数(){
var perflist='';
for(var执行者计时){
var j=获取(执行者);
perflist+='- '+performer+'是:'+j+'
';
}
性能列表+='
';
$(“正文”)。前置(性能列表);
$(“#adminmenu”).prepend(加载时间:“+loadTime+”秒 ”;
有人能帮我找出问题出在哪里吗?如果你查看其中的源代码,它在onload之后运行代码(setTimeout('WriteMings()',0)),你的代码在$(document).ready()中运行,它在onload之前运行,因为它在Chrome中的DOMContentLoaded上运行 我在您的代码中加入了setTimeout,现在它可以工作了:参见
var performance=window.performance | | window.mozPerformance | | | window.msPerformance | | | |{};
var timing=performance.timing | |{};
函数getTiming(计时str){
如果(!计时)返回0;
var时间=定时[定时STR];
返回时间;
}
var loadTime=(new Date().getTime()-getTiming(“navigationStart”))/1000;
$(文档).ready(函数(){
setTimeout(函数(){
var perflist='';
for(var执行者计时){
var j=获取(执行者);
perflist+='- '+performer+'是:'+j+'
';
}
性能列表+='
';
$(“正文”)。前置(性能列表);
$(“#adminmenu”).prepend(加载时间:“+loadTime+”秒 ”)
}, 100);
});
如果使用jQuery,最简单的方法是在loadEventEnd启动时调用函数
那么,替换
$(document).ready(function(){
与:
同样,在没有计时接口的浏览器中也会出现JS错误。在尝试执行之前,您需要使用类似的方法来验证接口是否存在。当浏览器确实具有该接口时,您可以使用上面的方法。否则,请尝试此方法进行检查:
$(window).load(function(){
if (window.performance != undefined) {
var e = window.performance;
if (e.timing) {
您看到
loadEventEnd:0
(以及loadEventStart:0
)的原因是您正在检查$(document).ready()中的值
$(document).ready()
是DOM完全加载的时间。这将在窗口的onLoad
事件触发之前发生,只有在下载了所有外部内容(例如CSS和图像)后才会发生
要更好地显示浏览器的功能,请参阅下图:
$(document).ready()
基本上是在domContentLoaded
之后、在domplete
之前以及在loadEventStart
和loadEventEnd
之前触发的(例如,窗口的onLoad
事件已经触发)
注意:您不应仅将代码更改为在窗口onLoad
事件期间运行,因为loadEventEnd
在onLoad
事件期间仍然为0。中的loadEventEnd
定义为:
此属性必须返回当前文档的加载事件完成的时间。当加载事件未触发或未完成时,它必须返回零
解决方案是在onLoad
事件期间忽略loadEventEnd
(并使用loadEventStart
或eventwindow.performance.now()
),或setTimeout(…,0)
在onLoad
事件期间,并在回调期间查询性能数据,因为它将在onLoad
之后,所以所有时间戳都应该填写。这实际上取决于时间戳对您的影响。您使用的是什么版本的Chrome?我问,因为我测试了14.0.794.0(Mac)使用此演示,我将loadEventStart视为一个值。@Kinlan当我点击页面时,得到的值如演示中所示,这就是我上面发布的代码的问题。但是,为什么“domplete”的值为0?
$(document).ready(function(){
$(window).load(function(){
$(window).load(function(){
if (window.performance != undefined) {
var e = window.performance;
if (e.timing) {