简体   繁体   English

如何使用JavaScript获得时间上的差异?

[英]How to get the difference in time using javascript?

im trying to get the current time timeNow in javascript and at the end of the script get timeEnd which is when the script finished executing for example: 我试图在javascript中获取当前时间timeNow ,并在脚本结尾处获取timeEnd ,这是脚本完成执行的时间,例如:

newDate = new Date();
timeNow = newDate.getTime();

for(i=0; i < 5; i ++){

    console.log("printing" + i);
}

timeEnd = timeNow - newDate.getTime();

console.log(timeEnd);

i want to get the time it took to excute this script in seconds or milliseconds, but the result is 0. i don't see why, can you help me thanks. 我想获取执行此脚本所需的时间(以秒或毫秒为单位),但结果为0。我不知道为什么,您能帮我谢谢吗。

The Date object does not keep ticking once it's constructed, it only represents a single instant in time. Date对象一旦构造就不会一直打勾,它仅表示一个时间点。

You need to re-evaluate the time at the end of the script and then compare that value with the first one. 您需要重新评估脚本末尾的时间,然后将该值与第一个值进行比较。

Note that on modern ES5 browsers you can use the "static" Date.now() function to get the current time. 请注意,在现代ES5浏览器上,您可以使用“静态” Date.now()函数来获取当前时间。 This is less expensive that constructing a whole new Date object. 这比构造一个新的Date对象便宜。 On older browsers you can emulate this function with this shim: 在较旧的浏览器上,您可以使用以下填充程序模拟此功能:

Date.now = Date.now || function() {
    return +(new Date);
};

At which point the required code becomes: 此时所需的代码将变为:

var start = Date.now();

// do your stuff here
...

var end = Date.now();

var elapsed = end - start;

FWIW, your simple loop reports 2ms when run on the Chrome console on my laptop. FWIW,在笔记本电脑的Chrome控制台上运行时,您的简单循环报告2毫秒。

This happens because you create only a single Date object. 发生这种情况是因为您仅创建了一个Date对象。 These don't change; 这些不变。 you will have to do: 您将需要执行以下操作:

timeEnd = timeNow - new Date().getTime();

(note the blank between new and Date ) (请注意newDate之间的空白)

var t = new Date().getTime();
// code
console.log(new Date().getTime() - t);

因为我想不到,执行5次迭代所需的时间不到一毫秒。

you can use 您可以使用

starttime = Number(new Date()); starttime = Number(new Date());

endtime = Number(new Date()); endtime = Number(新的Date());

I actually don't think it is iterating in less than an millisecond. 我实际上不认为它会在不到一毫秒的时间内进行迭代。 It think the problem is that when you call newDate.getTime() repeatedly it returns the same value each time. 它认为问题在于,当您重复调用newDate.getTime() ,每次都会返回相同的值。 The object, once you create it, is set at the time it was created. 创建对象后,将在创建对象时对其进行设置。 You'll need to create a new Date() object to compare the original against. 您需要创建一个新的Date()对象以将其与原始对象进行比较。

From my Firebug Console: 从我的Firebug控制台:

>>> d = new Date()
Date {Thu Aug 23 2012 10:45:52 GMT-0600 (Mountain Daylight Time)}
>>> d.getTime()
1345740352582
>>> d.getTime()
1345740352582
>>> d.getTime()
1345740352582
>>> d.getTime()
1345740352582

I can assure you that I didn't manually make the calls to d.getTime() in less than a millisecond. 我可以向您保证,我没有在不到一毫秒的时间内手动调用d.getTime()

Basically, Do this 基本上,这样做

newDate = new Date();
timeNow = newDate.getTime();

for(i=0; i < 50; i++){
    console.log("printing" + i);
}

futureDate = new Date()
timeEnd =  futureDate.getTime();
timeLength = timeEnd - timeNow;


console.log(timeLength);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM