4

我正在尝试为 iPad (iOS 5.1.1) 优化一个网络应用程序,我想知道如何衡量花在一个功能上的时间

在桌面上,firebug 和 webkit 的控制台都有console.time() 和 console.timeEnd()。不幸的是,我无法让它在 iOS 上运行,似乎只支持 console.log() 。

有什么选择吗?

4

3 回答 3

3
var start = new Date().getTime();

// ....

var end = new Date().getTime();

console.log(end-start);
于 2012-08-23T07:54:18.763 回答
0

根据xdazz的回答,我尝试替换这两个函数,以便它可以在 iOS 上运行,而无需重构所有计时器:

if(!console){var console = {};} // for those without a console - mind the context
console.timers = {};
console.time = function(timer)
{
    if(!timer){timer="Timer";}
    console.timers[timer] = new Date().getTime();
};
console.timeEnd = function(timer)
{
    if(!timer){timer="Timer";}
    console.log(timer+": "+(new Date().getTime()-console.timers[timer]));
};

警告:这样做将替换原始 API,因此可能会在您的桌面浏览器上提供不太准确的数据。

于 2012-08-23T08:40:45.987 回答
0

从运行 iOS 6 的 iPhone 4 CDMA 开始,移动 Safari 支持console.time()console.timeEnd().

于 2014-01-30T23:49:27.410 回答