Use of $interval and $timeout in AngularJS

We will have various scenarios such as timed refresh and lazy loading in our project.
Next, let's look at the use of $interval and $timeout

$interval can be used for timed tasks, we only need to inject $interval in the controller to use it. The following is the alert() function that is executed every ten seconds.


app.controller("ctrl", function($scope,$timeout,$interval) {
		$interval(function(){
alert("545")
		},10000);
	});

We can define the number of times the function is executed, and it will not be executed if it is executed three times as follows
app.controller("ctrl", function($scope,$timeout,$interval) {
		$scope.exe=$interval(function(){
alert("545")
		},10000,3);
	});

We can also terminate the execution of the timer as follows
$interval.cancel($scope.exe)


$timeout is used for lazy loading, which is relatively simple, as follows to delay loading for 1 second;
$timeout(function() {
			alert("454");
		}, 1000);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326374814&siteId=291194637