了解$ http查询获取结果所花费时间的最佳方法是什么? [英] Best way to know the time taken by a $http query to fetch the results?

查看:116
本文介绍了了解$ http查询获取结果所花费时间的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录$ http查询获取结果所花费的时间。一种解决方案是计算拨打电话之前和之后的时间差。那么,还有其他更好的方法来了解获取结果所需的时间吗?

I want to log the time taken by a $http query to fetch the results. One solution will be calculating the difference of time before and after making the call. So, is there any other better way to know the time taken to fetch the results?

推荐答案

你可以使用$ httpProvider的拦截器

You can use an interceptor for $httpProvider

app.factory('logTimeTaken', [function() {  
    var logTimeTaken = {
        request: function(config) {
            config.requestTimestamp = new Date().getTime();
            return config;
        },
        response: function(response) {
            response.config.responseTimestamp = new Date().getTime();
            return response;
        }
    };
    return logTimeTaken;
}]);
app.config(['$httpProvider', function($httpProvider) {  
    $httpProvider.interceptors.push('logTimeTaken');
}]);

然后你可以使用console.log或者你可以设置$ log,或者做你想要的数据。

And then you can console.log or you could setup $log, or do what you want with the data.

$http.get('url').then(function(response) {
    var time = response.config.responseTimestamp - response.config.requestTimestamp;
    console.log('Time taken ' + (time / 1000) + ' seconds.');
});

这篇关于了解$ http查询获取结果所花费时间的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆