[已解决]为什么JavaScript的"getTime()"计时时差为0?

软件和网站开发以及相关技术探讨
回复
科学之子
帖子: 2284
注册时间: 2013-05-26 6:58
系统: Debian 9

[已解决]为什么JavaScript的"getTime()"计时时差为0?

#1

帖子 科学之子 » 2016-09-15 1:40

为什么JavaScript的"getTime()"计时时差为0?
原来是自己理解有误
参考:http://www.daimajiayuan.com/sitejs-12541-1.html
测试工具:
http://www.w3school.com.cn/tiy/t.asp?f= ... te_gettime
代码不论循环多少次输出的时差都是0
甚至循环到运行了十几秒钟也是输出0
测试代码:

代码: 全选

<html>
<body>

<script type="text/javascript">
// Generated by gfwlist2pac
// https://github.com/clowwindy/gfwlist2pac

var proxy = "PROXY 127.0.0.1:80";

var domains = {
'googleusercontent.com':0,
'twitter.com':0,
'googleapis.com':0,
'google.com.hk':0,
'blogblog.com':0
};

var direct = 'DIRECT';

var hasOwnProperty = Object.hasOwnProperty;

function FindProxyForURL(url, host) {
    var suffix;
    var pos = host.lastIndexOf('.');
    pos = host.lastIndexOf('.', pos - 1);
    while(1) {
        if (pos <= 0) {
            if (hasOwnProperty.call(domains, host)) {
                return proxy;
            } else {
                return direct;
            }
        }
        suffix = host.substring(pos + 1);
        if (hasOwnProperty.call(domains, suffix)) {
            return proxy;
        }
        pos = host.lastIndexOf('.', pos - 1);
    }
}


var d=new Date();
start_t=d.getTime();
for(var i=0;i<1000000;i++)
FindProxyForURL('https://www.google.com/','www.google.com');
end_t=d.getTime();
document.write(end_t-start_t);
</script>

</body>
</html>
头像
AutoXBC
帖子: 1744
注册时间: 2007-10-23 12:54

Re: [已解决]为什么JavaScript的"getTime()"计时时差为0?

#2

帖子 AutoXBC » 2016-09-16 1:47

如果仅仅计算时间差,可以用两个 Date 对象直接相减,不需要 getTime,精度也是毫秒。
头像
Ubuntu与Linux
帖子: 1211
注册时间: 2010-06-09 19:57

Re: [已解决]为什么JavaScript的"getTime()"计时时差为0?

#3

帖子 Ubuntu与Linux » 2016-09-19 23:37

d.getTime()===d.getTime()
头像
binbinxyz
帖子: 5
注册时间: 2012-03-19 10:42

Re: [已解决]为什么JavaScript的"getTime()"计时时差为0?

#4

帖子 binbinxyz » 2016-10-23 15:28

start_t,end_t值都是从同一个时间d来获取的,当然是一样的啊;要是不一样才奇怪呢!
回复