HTML DOM console.time()方法用法示例介绍

console.time()HTML中的方法用于在控制台视图中启动计时器。的console.time()该方法可用于计算各种测试目的的程序时间。标签将作为参数发送到console.time()方法.
语法如下:

console.time( label )

参数:此方法接受单个参数标签这是可选的, 用于指定计时器的标签。
以下程序说明了HTML中的console.time()方法:
范例1:
< !DOCTYPE html> < html> < head> < title> DOM console.time() Method in HTML< /title> < style> h1 { color: green; }h2 { font-family: Impact; }body { text-align: center; } < /style> < /head> < body> < h1> srcmini< /h1> < h2> DOM console.time() Method< /h2> < p> To view the message in the console press the F12 key on your keyboard. < /p> < p> To view the time taken by a for loop to run 100 times, double click the button below: < /p> < br> < button ondblclick = "table_time()"> RUN TIMER < /button> < script> function table_time() { console.time(); for (i = 0; i < 100 ; i++) { //Random code } console.timeEnd(); } < /script> < /body> < /html>

输出如下:
HTML DOM console.time()方法用法示例介绍

文章图片
控制台视图:
HTML DOM console.time()方法用法示例介绍

文章图片
范例2:使用console.time()方法计算while循环所花费的时间
< !DOCTYPE html> < html> < head> < title> DOM console.time() Method in HTML< /title> < style> h1 { color: green; }h2 { font-family: Impact; }body { text-align: center; } < /style> < /head> < body> < h1> srcmini< /h1> < h2> DOM console.time() Method< /h2> < p> To view the message in the console press the F12 key on your keyboard. < /p> < p> To view the time taken by a for loop to run 100 times, double click the button below: < /p> < br> < button ondblclick = "table_time()"> RUN TIMER < /button> < script> function table_time() { i = 0; console.time ("While loop for 100 iterations"); while (i < 100 ) { i++; } console.timeEnd ("While loop for 100 iterations"); } < /script> < /body> < /html>

输出如下:
HTML DOM console.time()方法用法示例介绍

文章图片
控制台视图:
HTML DOM console.time()方法用法示例介绍

文章图片
【HTML DOM console.time()方法用法示例介绍】支持的浏览器:支持的浏览器console.time()方法下面列出:
  • 谷歌浏览器
  • Internet Explorer 11.0
  • Firefox 10.0
  • 歌剧
  • Safari 4.0

    推荐阅读