python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
$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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions