Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trying to prove that it's not possible to set a default timeout in angular.js
<!doctype html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
</head>
<body>
<div ng-controller="TodoCtrl">
<button ng-click="beQuickOrBeDead()">Fetch with 100ms timeout</button>
<button ng-click="neverEndingStory()">Fetch with default timeout</button>
<p>{{status}}</p>
</div>
<script>
var myApp = angular.module('myApp', []);
myApp.config(['$httpProvider', function ($httpProvider) {
console.log("Set config");
$httpProvider.defaults.timeout = 150;
}]);
myApp.controller('TodoCtrl', function ($scope, $http) {
$scope.status = "N/A";
$scope.neverEndingStory = function () {
$scope.status = "N/A";
console.log($http.defaults); // The value is set to 150 but it doesn't affect the .get
$http.get("/longrunningrequest", { })
.success(success)
.error(error);
};
$scope.beQuickOrBeDead = function () {
$scope.status = "N/A";
console.log($http.defaults); // The value is set to 150 but it doesn't affect the .get
$http.get("/longrunningrequest", { timeout: 100 })
.success(success)
.error(error);
};
function success() {
$scope.status = "OK";
console.log("Request ok");
}
function error() {
$scope.status = "NOT OK";
console.log("Request not ok");
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment