简体   繁体   English

拦截器中的角度http请求时间

[英]Angular http request time in interceptor

I'm making an interceptor to log my http requests. 我正在做一个拦截器来记录我的http请求。

So far, so good, everything is working as expected. 到目前为止,一切都很好,一切正常。

What I want now is to get the time the request took to be executed. 我现在想要的是获取执行请求所花费的时间。

I thought I could do something like this 我以为我可以做这样的事情

const start = Date.now();
return next
  .handle(req)
  .map(res => {
    console.log('took ' + (Date.now() - start) + 'ms');
    return res;
  })

} }

But the console shows 1 to 2ms, while the network shows more than 50ms ... I think that I should create the start value right when I create the request, but I don't know how. 但是控制台显示1到2毫秒,而网络显示超过50毫秒...我认为我应该在创建请求时立即创建起始值,但是我不知道如何。

Any solution ? 有什么办法吗?

PS : my linting config forbids me to use console.time() PS :我的配置配置禁止我使用console.time()

use performance.now() to measure time duration in milliseconds 使用performance.now()来测量持续时间(以毫秒为单位)

var start = performance.now(); 

return next
  .handle(req)
  .map(res => {
    console.log('took ' + (performance.now() - start) + 'ms');
    return res;
  })

For futher info check this 有关更多信息,请检查

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM