Cookies on this site

We would like to use cookies to understand how the site is used and to measure our campaigns. Nothing is set until you choose, and you can change your mind at any time from “Cookie settings” at the foot of every page. Privacy policy

Contact UsContact Us

Resources / Blogs

Cloud & apps

AngularJS timeout and interval services

Angular JS scheduling services - The timeout service can be used to call another JavaScript function after a given time delay. Find out what more you can get from Angular JS timeout and interval services.

· · 2 min read

AngularJS

It is not always required of our functions to provide immediate results, sometimes we need to delay the changes made by a function, for eg, processing of an AJAX request or if we want to schedule changes to an animation. To such end AngularJS provides us with $timeout( ) and $interval( ) services.
To use these services, as usual, we have to inject the dependency into the controller

var myapp = angular.module("myapp", []);
myapp.controller("MyController", ['$scope','$timeout',function($scope, $timeout){
}]);

The $timeout service can be used to call another JavaScript function after a given time delay. The $timeout service only schedules a single call to the function. To invoke a function repeatedly we have to use the $interval( ) service. Both these services work in a similar manner, barring this functionality.
Once the $timeout service is injected into your controller function, you can use it to schedule function calls. For example:

myapp.controller("MyController", ['$scope','$timeout',function($scope, $timeout){
$timeout(callThis,2000);
}]);
function callThis(){
console.log("I am called after 2 seconds");
}

Here the time is to be entered in milliseconds. We can also call functions on the $scope object:

myapp.controller("MyController", ['$scope','$timeout',function($scope, $timeout){
$scope.callThis = function() {
console.log("I am called after 2 seconds from scope");
}
$timeout(function(){
$scope.callThis();
},2000);
}]);

All the above is also true for the $interval( ) service.

Now an important thing to note is if the function we schedule for execution makes changes to variables in the $scope object, or make changes to any other variable which our application is watching, the application needs to execute $scope.$digest( ) after the scheduled function call finishes. It is the default behavior of AngularJS to automatically call $scope.$digest( ) but it can get tricky if we are juggling through scopes inside an ngRepeat loop. For remedy we have an optional third boolean parameter in both the $timeout( ) and $interval( ) services which lets us explicitly specify whether to call the function scheduled inside a $apply( ) block or not. This sometimes helps us in avoiding the $inprog error which complains that we tried to execute a $apply in the middle of another $digest cycle.

← All articles

Case studies

Cloud Consulting in practice

Client names are withheld. We are happy to share details on a call.

All case studies
  • Delivered

    On-prem → Amazon RDS

    Nonprofit

    Cloud Consulting

    Oracle from on-premises to Amazon RDS for a national nonprofit

    The nonprofit's on-premises Oracle database had become hard to maintain and scale. We planned the move with automated workflows and validation tests, migrated to Amazon RDS for Oracle with minimal disruption and left a repeatable process for future changes.

    • Automated, tested migration
    • Less day-to-day database upkeep
  • Delivered

    600k files, < 3 days

    Document management

    Microsoft

    600,000 documents moved from ImageRight to SharePoint in under three days

    A one-file-at-a-time script would have needed about 22 days of continuous running. We rebuilt the migration around a SQL work queue with parallel uploads, automatic retries and crash recovery, and it finished unattended in under three days.

    • 1,125 → 8,314 files per hour
    • Under 3 days instead of about 22
  • Delivered

    Every change, flagged

    Food manufacturing

    AI Consulting

    Label comparison agent for a national food cooperative

    Compares two versions of a product label, highlights every change, annotates the PDF and returns an audit-ready list of differences. We moved it from Copilot Studio to Azure Functions when the document processing outgrew the low-code platform.

    • Audit-ready differences in one chat turn
    • Rebuilt from low-code to cloud functions

Keep reading

All Cloud & apps articles

Moving an application to the cloud?

Request a migration plan consult. We will scope the move with you and come back with a fixed-scope plan.