$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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book