user153882
user153882

Reputation: 347

How to set delay timeout for each HTTP get on each button request

I have a button which fetches data from database.php. I'm trying to add a timeout delay BEFORE the http.get not response, so I can't use promise. In php, its so easy by using sleep(). As for the reason, its part of another project to simulate latency (I know you can do it via other means).

app.controller('mainController', function ($scope, $http, $timeout) {
     $scope.requestData = function () {
        $scope.delta = '[waiting]';
        //Delay here!
        $http.get('database.php').then(function (response) {
            $scope.rows = response.data.records;
    };
});

I have tried, doesn't work:

app.controller('mainController', function ($scope, $http, $timeout) {
     $scope.requestData = function () {
        $scope.delta = '[waiting]';
        $http.get('database.php',{ timeout: 3000 }).then(function (response) {
            $scope.rows = response.data.records;
    };
});

I have tried passing an empty timer, doesn't work:

app.controller('mainController', function ($scope, $http, $timeout) {
    var timer = function () {
    }

    $scope.requestData = function () {
        $scope.delta = '[waiting]';
        $timeout(timer, 3000);
        $http.get('database.php').then(function (response) {
            $scope.rows = response.data.records;
    };
});

Any ideas, what I can do?

Upvotes: 0

Views: 887

Answers (1)

Agam Banga
Agam Banga

Reputation: 2693

You need to pass the function which you want to delay into the timeout function like this

app.controller('mainController', function ($scope, $http, $timeout) {
     $scope.requestData = function () {
        $scope.delta = '[waiting]';
        //Delay here!
        $timeout(function () {
            $http.get('database.php').then(function (response) {
                 $scope.rows = response.data.records;
            });
        }, 2000)
    };
});

Upvotes: 2

Related Questions