分页: 1 / 1

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

发表于 : 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>

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

发表于 : 2016-09-16 1:47
AutoXBC
如果仅仅计算时间差,可以用两个 Date 对象直接相减,不需要 getTime,精度也是毫秒。

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

发表于 : 2016-09-19 23:37
Ubuntu与Linux
d.getTime()===d.getTime()

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

发表于 : 2016-10-23 15:28
binbinxyz
start_t,end_t值都是从同一个时间d来获取的,当然是一样的啊;要是不一样才奇怪呢!