$interval vs. $timeout angularjs

$interval vs. $timeout angularjs

What is $timeout

The $timeout is used to call another controller function in a given time frame that means scheduling a function calls.

$timeout service should be injected into a controller.

$timeout used for single call to the function but $interval used for schedules for multiple repeated call in a time interval.

The demo example code for $timeout as given below.

var app = angular.module("IntervalApp", []);

// $timeout service should be injected into a controller function.
app.controller("IntervalCtrl", function($scope, $timeout) {

    $scope.count = 0;

    $scope.schedulingForTimeout = function() {

        $scope.count += 1;

        // Call the $timeout function call after 5 seconds. Its single call after 5 second.
        $timeout(function() {
            $scope.schedulingForTimeout();
        }, 1000);
    }

    $timeout(function() {
        $scope.schedulingForTimeout();
    }, 1000);


});


The demo example code for $Interval as given below.

var app = angular.module("IntervalApp", []);

// $timeout service should be injected into a controller function.
app.controller("IntervalCtrl", function($scope, $interval) {

    //Initialize the counter.
    $scope.count = 0;

    $scope.schedulingForInterval = function() {
        $scope.count += 1;
    };

    // the $interval call every 1 second itself.
    $interval(function() {
        $scope.schedulingForInterval();
    }, 1000);

});

The output go to below link  

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.