简体   繁体   English

从单击按钮到网站收到数据的确切时间距离

[英]The Exact time distance from button clicked to when received data to a website

I'm creating a chrome extension.我正在创建一个 chrome 扩展。 I need to know when a user clicked on a specific button how many mili seconds will take long, to receive that command to website server.我需要知道当用户单击特定按钮时需要多少毫秒才能将命令接收到网站服务器。 I have a web Worker that is connected to that website too.我也有一个连接到该网站的 web Worker。 Could I get the exact time when a button clicked to when data received by website's server?我能否获得网站服务器收到数据时单击按钮的确切时间? It doesn't matter how many mili seconds take that the respond back to me, the time of receiving request to server after click is mu issue now.回复我需要多少毫秒并不重要,点击后接收到服务器请求的时间现在是 mu 问题。 can someone help me please?有谁可以帮助我吗?

I'm looking for javascript code to get the time distance between button clicked and received that by website's server.我正在寻找 javascript 代码来获取单击按钮和网站服务器接收按钮之间的时间距离。

It is easy to get the time when button is clicked by finding that button and attaching an event handler on it, that calls performance.now()通过找到该按钮并在其上附加一个调用performance.now()的事件处理程序,很容易获得单击按钮的时间

const t0 = 0;
document.getElementById('buttonId').addEventListener('click',() => {
  t0 = performance.now();
});

Detecting when the server response is received is not trivial.检测何时收到服务器响应并非易事。 You might need to use the chrome.webRequest api.您可能需要使用chrome.webRequest api。

Alternatively, a simpler way would be to look for side-effects of the request, that happen in the DOM.或者,一种更简单的方法是查找请求的副作用,这些副作用发生在 DOM 中。 (A loader appearing and disappearing, data rows appearing, button being disabled and re-enabled, etc). (加载程序出现和消失,数据行出现,按钮被禁用和重新启用等)。

You can either poll for these changes, or use the mutationObserver api to detect when elements containing expected attributes are available in the DOM.您可以轮询这些更改,或使用mutationObserver api 来检测包含预期属性的元素何时在 DOM 中可用。

Let's say you are polling for the button being re-enabled, every 10ms:假设您每 10 毫秒轮询一次重新启用的按钮:

const t1 = 0;
const interval = window.setInterval(() => {
  if (!document.getElementById('buttonId').getAttribute('disabled')) {
    t1 = performance.now();
    requestTime = t1 - t0;
    console.log(requestTime);
    window.clearInterval(interval);
  }
},10);

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

相关问题 从网站单击按钮时如何在Firefox附加组件中触发功能 - How to Fire a function in the Firefox addon when button is clicked from the Website 如何在单击按钮后禁用按钮并在从后端代码收到响应时将其重新启用 - how to disable a button in react after it is clicked and enable it back when a response is received from backend code 单击“打印”按钮时获取jsp页面的确切布局 - Getting the exact layout of jsp page when print button is clicked 在Meteor JS中单击按钮时显示来自集合的数据 - Display data from a collection when button is clicked in Meteor JS 在父级中单击按钮时如何将数据从子级传递给父级 - How to pass data from child to parent when button clicked in parent 当单击其他网站上的网页上的按钮时,刷新外部网页 - refreshing an external web page when a button is clicked on a web page from a different website 使用 Javascript 或 Jquery 单击按钮时,如何从网站发送消息? [等候接听] - How to send a message from a website when a button is clicked using Javascript or Jquery? [on hold] 第二次单击时按钮被禁用 - Button gets disabled when clicked second time 使用javascript在php网站中单击按钮时显示加载圈? - Display loading circle when button is clicked in php website using javascript? 单击按钮时尝试将数据添加到数组 - trying to add data to an array when button is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM