javascript - Angular http request time in interceptor - Stack Overflow

I'm making an interceptor to log my http requests. So far, so good, everything is working as expec

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745095967a4610979.html

相关推荐

  • javascript - Angular http request time in interceptor - Stack Overflow

    I'm making an interceptor to log my http requests. So far, so good, everything is working as expec

    14小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信