mahesh reddy
mahesh reddy

Reputation: 377

Angular Timeout calling function without delay

In the following snippet, the progress call is getting triggered continuously with out the 10s delay. Please let me know where i am going wrong

        ctrl.progress =function (fileName){

            if(ctrl.status < 100){
                ctrl.timer = $timeout(function(fileName){

                    LookUpValueService.getImportStatus(fileName).
                    then(function(value){
                        ctrl.status = value; 
                        ctrl.progress(fileName);
                    });
                    //ctrl.status = ctrl.status + 1;                

                }(fileName),5000);
            }
            else{
                $timeout.cancel(ctrl.timer);
            }
        }; ctrl.progress("test.txt");

Upvotes: 1

Views: 184

Answers (1)

GregL
GregL

Reputation: 38151

The first argument to $timeout() has to be a function itself, not the result of a call to a function.

In your case, ctrl.progress() does not return a function, it returns undefined, so when you have:

ctrl.timer = $timeout(ctrl.progress(fileName), 10000);

what happens is that ctrl.progress(fileName) gets called immediately, returns undefined, and then you tell $timeout() to wait 10 seconds before doing undefined.

Change that line to use a function instead, and it will work:

ctrl.timer = $timeout(() => ctrl.progress(fileName), 10000);

(or if not using ES6/2015/TypeScript: ctrl.timer = $timeout(function () { ctrl.progress(fileName) }, 10000);)

Upvotes: 2

Related Questions