1

I am trying to add a delay to an Angular .get() request so I can simulate latency or a slow network. I want to test how timer animations are displayed etc.

Does anyone know how I can achieve this?

Here is a sample of my .get()

 $scope.loading = true;


    // get all the portfolios and the products
    //http.get('scripts/json/sample-products.json')
    $http.get('http://www.website.com/Pricing/GetProductCatalogue')
        .then(function (allProducts) {
            $scope.listOfProducts = allProducts.data.Connectivity;
        })
        .finally(function() {
            $scope.loading = false;
        });

The scope loading would be the div I use to contain my loading animations and messages.

2 Answers 2

2

you can add a timeout (with $timeout or window.setTimeout) in the succes function, like this:

$scope.loading = true;


    // get all the portfolios and the products
    //http.get('scripts/json/sample-products.json')
    $http.get('http://www.website.com/Pricing/GetProductCatalogue')
        .then(function (allProducts) {
            $timeout(function(){
                $scope.listOfProducts = allProducts.data.Connectivity;
            },10000)

        })
        .finally(function() {
            $scope.loading = false;
        });
0
0

Use $timeout

$scope.loading = true;

var getFunc = function(){
    $http.get('http://www.website.com/Pricing/GetProductCatalogue')
    .then(function (allProducts) {
        $scope.listOfProducts = allProducts.data.Connectivity;
    })
    .finally(function() {
        $scope.loading = false;
    });
}

$timeout(getFunc, 1000);

And do not forget to inject $timeout into your controller

1
  • with this timeout you are not simulating the delay in the response, but you are delaying the execution of the call... Feb 2, 2015 at 15:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.