Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 了解事件的时间来源';时间戳_Javascript_Dom Events_High Resolution Time - Fatal编程技术网

Javascript 了解事件的时间来源';时间戳

Javascript 了解事件的时间来源';时间戳,javascript,dom-events,high-resolution-time,Javascript,Dom Events,High Resolution Time,最近开始使用他们的。与前一种方法的主要区别之一是,此持续时间与系统历元无关,而是与(大多数时间大约是加载文档的时间)相关 问题是,我找不到任何可靠的方法来检查浏览器是否使用高分辨率时间戳或以前的方法 如果只比较事件时间戳,这不是问题,但如果要将事件时间戳与手工制作的时间戳进行比较,则需要知道是否应使用Date.now()(相对于历元)或precision.now()(相对于时间原点) 例如,虽然safari mobile支持性能api,但某些版本(如iOS 11.1中的版本)尚未将其事件切换为高

最近开始使用他们的。与前一种方法的主要区别之一是,此持续时间与系统历元无关,而是与(大多数时间大约是加载文档的时间)相关

问题是,我找不到任何可靠的方法来检查浏览器是否使用高分辨率时间戳或以前的方法

如果只比较事件时间戳,这不是问题,但如果要将事件时间戳与手工制作的时间戳进行比较,则需要知道是否应使用
Date.now()
(相对于历元)或
precision.now()
(相对于时间原点)

例如,虽然safari mobile支持性能api,但某些版本(如iOS 11.1中的版本)尚未将其事件切换为高分辨率时间戳。 因此,下面的代码段为
差异
生成了无意义的巨大值,因为
开始
结束时是一个高分辨率的时间戳,而不是事件的时间戳

const now=性能?()=>performance.now():()=>Date.now();
让我们开始;
常数更新=e=>{
如果(e)e.preventDefault();
document.querySelector(“#start”).innerHTML=start;
document.querySelector(“#事件”).innerHTML=e?e.timeStamp:”;
document.querySelector('#diff').innerHTML=e?e.timeStamp-start:'';
};
常数重置=()=>{
开始=现在();
更新();
}
重置();
document.querySelector(“#reset”).addEventListener('click',reset');
document.querySelector(“#pad”).addEventListener('mousemove',update);
document.querySelector(“#pad”).addEventListener('touchmove',update)
#pad{
背景色:#C9DBFA;
边框:1px实心#778294;
边界半径:10px;
宽度:500px;
高度:100px;
颜色:#575E6C;
填充物:5em.75em;
}
#重置{
利润率:1米0;
}

开始时间:

事件时间:
差异:
重置
假设浏览器对所有时间戳都一致地使用相同的机制,我建议只需触发一个虚拟事件来预先进行测试:

var isPerformanceTimestamp = false;
if (performance) {
    var performanceNow = performance.now();
    document.addEventListener('test-performance', function(evt) {
        isPerformanceTimestamp = evt.timeStamp - performanceNow < 1000; // or whatever threshold you feel comfortable with, it's at least 1530355731395 at the time of this writing ;-)
        document.removeEventListener('test-performance', this);
    });
    var event = new Event('test-performance');
    document.dispatchEvent(event);
}
var isPerformanceTimestamp=false;
if(性能){
var performanceNow=performance.now();
文件.附录列表器(“性能测试”,功能(evt){
isPerformanceTimestamp=evt.timeStamp-performanceNow<1000;//或者您觉得合适的阈值,在撰写本文时至少是1530355731395;-)
文件。删除EventListener(“测试性能”,本文件);
});
var事件=新事件(“测试性能”);
文件、调度事件(事件);
}

我最终修改了从中提取的解决方案。这个想法实际上非常类似于,但有更多的保护措施:

const context = global || window || self;

const doEventUseHighResTimeStamps = () => {
  // If the performance API isn't even available, I assume events will not
  // use high res time stamps.
  if ('performance' in context) return false;

  // Check if the browser supports CustomEvents.
  const hasCustomEvent =
    'CustomEvent' in context &&
    (typeof context.CustomEvent === 'function' ||
      context.CustomEvent.toString().indexOf('CustomEventConstructor') > -1);

  // Create an event and compare its timestamps to the value returned by
  // performance.now(). If the event is using old timestamps, its origin
  // is going to be very far and hence, its timestamps will be much much
  // greater than performance.now().
  const testTimeStamp = hasCustomEvent
    ? new context.CustomEvent('test').timeStamp
    : context.document.createEvent('KeyboardEvent').timeStamp;
  return testTimeStamp && testTimeStamp <= performance.now();
};

开始时间:

事件时间:
差异: 重置