最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular http request time in interceptor - Stack Overflow

programmeradmin1浏览0评论

I'm making an interceptor to log my http requests.

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.

Any solution ?

PS : my linting config forbids me to use console.time()

I'm making an interceptor to log my http requests.

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.

Any solution ?

PS : my linting config forbids me to use console.time()

Share Improve this question edited Jan 5, 2018 at 14:37 asked Jan 5, 2018 at 14:32 user4676340user4676340
Add a ment  | 

1 Answer 1

Reset to default 6

use performance.now() to measure time duration in milliseconds

var start = performance.now(); 

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

For futher info check this

发布评论

评论列表(0)

  1. 暂无评论