var app = angular.module('app', []);
app.controller('MainController', function($scope, $timeout) {
$scope.message = 'No timeout scheduled.';
$scope.delay = 2000;
$scope.timeoutPromise = null;
$scope.startTimeout = function() {
$scope.message = '';
$scope.cancelTimeout();
$scope.message = 'Starting a new Timeout';
$scope.timeoutPromise = $timeout(function(data) {
$scope.message = 'Timeout complete.';
$scope.timeoutPromise = null;
}, $scope.delay, true, {data: 'testing123'});
};
$scope.cancelTimeout = function() {
if ($scope.timeoutPromise) {
$timeout.cancel($scope.timeoutPromise);
$scope.timeoutPromise = null;
$scope.message = 'cancelling previous Timeout';
}
};
});
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="MainController">
<p>
<button ng-click="startTimeout()">Start Timeout</button>
</p>
<p>
<button ng-click="cancelTimeout()">Cancel Timeout</button>
</p>
<p>Delay:
<input type="text" ng-model="delay" />
</p>
<hr />
<p>{{ message }}</p>
</div>
</body>
</html>
/* Put your css in here */