[英]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.