1

I want to add some delay in angularjs. So that it can fetch data from api. Because my api is heavy. I am calling this function. Basically i want some delay to load page so that my api work properly. I tried some time my data pull immediate some time i am getting error. When i refresh my page it work fine.

      Help to put $timeout function in this angularjs
  // Geting test stuff
    $scope.teststuff = function () {
$scope.loading = true;
$http.get(settings.WebApiBaseUrl + 'Api/testing')           
   .success(function (data) {
        $scope.mydata = data;
        $scope.loading = false;
    }).error(function (data, status, headers, config) {
        alert("Error " + status )             
  $scope.loading = false;
    })      

}

6
  • you can increase service timeout as per your requirement; Commented Aug 10, 2017 at 17:35
  • why do u want delay ..I believe your teststuff function will get called immediately when page loads ! Commented Aug 10, 2017 at 17:36
  • 1
    Adding arbitrary $timeout delays is a terrible way to cope with async code. Use a resolved Promise to control when the code depending on the AJAX call runs, or design your components such that they can cope with empty data while the ajax call is still in progress. Commented Aug 10, 2017 at 17:38
  • You should use a promise... Commented Aug 10, 2017 at 17:51
  • whats the use-case for the delay Commented Aug 10, 2017 at 18:00

1 Answer 1

0

Updated:

This problem can easily be solved by using resolve property of $routerProvider. Please use this config. So here the route /ed will not load until the resolve is completed, meaning the http request must return a value, only after that the page will load.

app.config(['$routeProvider', '$httpProvider', function ($routeProvider, $httpProvider) {
    var settings.WebApiBaseUrl =  "some url you want to use";
    $routeProvider.when("/al", { templateUrl: "Views/test.html", controller: "testCtrl" })
  .when("/ed", { 
    templateUrl: "Views/test2.html", 
    controller: "test2Ctrl",
    resolve: {
        initialize: function($http, $sessionStorage){
            $http.get('api/ApiKey/' + $sessionStorage.user)
          .success(function (myKey) { 
            return $http.get(settings.WebApiBaseUrl + 'Api/test1', { headers: { 'X-ApiKey': myKey } })
            .success(function (data) { 
              return data; 
            }).error(function (data, status, headers, config) { 
              alert("error" + status);
            });
          }).error(function (data, status, headers, config) { 
             alert("error" + status);
          }); 
        }
    }
  })
  .otherwise({ 
    redirectTo: '/al' 
  }); 
}]);

Now in the controller you need to do.

app.controller( 'test2Ctrl', function ( $scope, initialize ) {
        $scope.loading=false;
        $scope.test1=initialize;
        $scope.loading=true;
});

Old Answer: so you have a http request which is a promise and will wait until the data is received and from your example I can see you are implementing a loading screen kind of thing until the http request is completed.

To ensure the bulky http request doesn't time out you can use.

angular.module('MyApp', [])
  .config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.timeout = 5000;
}]);

While the page waits for the http request to complete you can use the scope variable($scope.loading = true;) to activate a loading spinner library to mask your page. Checkout angular loading overlay or some other code to do this.

Reference: http timeout

Sign up to request clarification or add additional context in comments.

11 Comments

I want to delay every single call (endpoint)? When they are called.
If you have an $http request it uses promises to wait for the data to be received so there is no need for setting the delay, are you using any angular router (ui-router) in you application? The line I have given will increase the wait time for the HTTP request, I gave this because I thought the request is getting timed out.
@DeveshKhosla angular has a $routeprovider service, there you can place your http request in a resolve parameter, the page will not load until the http request is completed. embed.plnkr.co/kHXK54 also odetocode.com/blogs/scott/archive/2014/05/20/…
Same problem. Actually I have problem with APIKey. Some time my APIKey work fine some time it give me error 401. That's why i was thinking to put some delay. But still same problem
@DeveshKhosla you are in the wrong approach, delay will only worsen your code quality, please share your angular routing config, if possible share your code, I will show what's wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.