elapsed-time.js

来自「这是《JavaScript学习源代码》一书的源代码」· JavaScript 代码 · 共 21 行

JS
21
字号
// create a new Date object then get the current time
var start = new Date();
var msec1 = start.getTime();

// run a loop counting up to 250,000
var num = 0;
for( var i = 0; i < 250000; i++ )
{
  num++;
}

// get another Date object and get the time now
var stop  = new Date();
var msec2 = stop.getTime();

// subtract the start time from the stop time to 
// discover the amount of time between the two points
var diff = ( msec2 - msec1 ) / 1000;

// display the elapsed time
alert("Time elapsed: " +diff+ " seconds");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?