I am using http interceptor directive to load the spinner and here is my script code:
createProfileModule.config(['$httpProvider', function ($httpProvider) {
var $http,
interceptor = ['$q', '$injector', function ($q, $injector) {
var error;
function success(response) {
// get $http via $injector because of circular dependency problem
$http = $http || $injector.get('$http');
if($http.pendingRequests.length < 1) {
$('#loadingWidget').hide();
}
return response;
}
function error(response) {
// get $http via $injector because of circular dependency problem
$http = $http || $injector.get('$http');
if($http.pendingRequests.length < 1) {
$('#loadingWidget').hide();
}
return $q.reject(response);
}
return function (promise) {
$('#loadingWidget').show();
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
}]);
and in HTML i am calling the div like this with the required CSS
<div id="loadingWidget" style="display:none;"></div>
<style>
#loadingWidget { position: absolute; z-index:2;
top: 0;
left: 0;
height:100%;
width:100%;
background: #000000 url(/ERegII-1.0/images/ajax-loader.gif) no-repeat center;
cursor: not-allowed;
filter: alpha(opacity=60);
opacity: 0.6;
content:'Loading..';
background-size:100px 100px;
}
</style>
What I want to make this spinner smart. I don't want to display the spinner on every small http call. So if my request if really small and it takes some milliseconds, I don't want to display the spinner. I want to add a delay time to the spinner.. let say 2 second. So if the request it taking more than 2 second this spinner should show up otherwise I don't want to display it. Can someone help me to find the solution for this? I have tried https://github.com/ajoslin/angular-promise-tracker but seems like doesn't work for me.
Can someone help me to make it smarter please?