Skip to content

Instantly share code, notes, and snippets.

@leandromoreira
Forked from adnan-i/angular-timeout.html
Created August 4, 2013 23:01
Show Gist options
  • Save leandromoreira/6152411 to your computer and use it in GitHub Desktop.
Save leandromoreira/6152411 to your computer and use it in GitHub Desktop.
<!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', []);
// this custom config could actually be a part of a more general app-level config
// so that you need to inject only one global config
myApp.value('http_defaults', {
timeout: 150
});
myApp.controller('TodoCtrl', function ($scope, $http, http_defaults) {
$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
console.log(http_defaults); // Custom config object for http calls
// in case you need to change the default values for this specific request
// angular.extend(http_defaults, {timeout: 300});
$http.get("/home", http_defaults)
.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("/home", { 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