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?
Asked
Active
Viewed 4,746 times
2
-
If you do not want to log it just want to see it. Use Chrome Developer tool Network Tab. If you want to log it then best way is time difference calculation I belive. – Partha Sarathi Ghosh Nov 05 '15 at 06:29
-
okay. i want to log it. – SaiGiridhar Nov 05 '15 at 06:34
-
If one or more answers are correct you should accept (and upvote if appropriate) one so other users of this site know which one is best and correctly answered your question. – Tristan Nov 27 '15 at 21:21
1 Answers
8
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');
}]);
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.');
});

Tristan
- 3,301
- 8
- 22
- 27