0

I need to throw error when api doesn't respond within 30 seconds.

$http.get("api/data.json",{timeout : 3000}).success(function(data){
            $scope.data = data;
        }).error(function(data) {
            console.log('api didnt respond in 30 seconds')
        });

But it doesnt land in the error block even after 30 seconds. I am new to angularjs. Please guide me.

1 Answer 1

1

You need to pass a promise to the $http timeout property:

    app.controller('ctrl', function ($http, $timeout) {
        $http({
            method: 'GET',
            url: 'api/data.json',
            timeout: $timeout(function () { alert('request timed out'); }, 3000)
        }).success(function (data) {
            alert('request successful');
        });
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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