2013-03-08 84 views
4

嗨,我正在使用jQuery。如何在jquery中获取事件的完成时间

我有一个复选框,单击事件

jQuery('.btn-check-box').click(function() { 

    //code goes here . 
}); 

问题是click事件需要大量的时间来complete.some倍的浏览器卡住。 所以我想改变函数内的代码并减少完成时间。

现在我想获得我当前的点击事件完成时间。 然后更改代码并再次检查完成时间。

所以我可以根据完成时间优化我的代码。

有什么办法来检查事件完成时间。 请帮忙。 在此先感谢。

+2

下面你就不能像'finishtime,starttime'? – SRy 2013-03-08 06:22:22

回答

6

传递事件作为参数。

var createdTime; 
jQuery('.btn-check-box').click(function(e){ 
    createdTime = e.timeStamp; 
}).done(function(){ calculate time difference }); 

,如果你会使用console.log(e)那么它会告诉你,jQuery的已经通过时间戳时触发的事件。

所以现在你需要找到一种方式,即使这样做.done() jQuery方法将帮助你。

在侧面.done()你也可以得到相同的e.timestamp并做一些数学运算来获得你想要的输出秒,分钟或任何东西。

1

就像SrinivasR说的,完成时间 - 开始时间。

jQuery('.btn-check-box').click(function() { 
    var t0 = (new Date()).getTime(); 
    //code goes here 
    console.log("Event took",(new Date()).getTime() - t0, "ms to complete."); 
}); 
1

尝试为您的代码

jQuery('.btn-check-box').click(function() { 
var secondStart = new Date().getTime()/1000; 
    // Your code goes here . 
var secondEnd = new Date().getTime()/1000; 
var totalTime = secondEnd - secondStart; 
console.log('Executing took' +totalTime +'seconds') 
}); 
相关问题