1

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");

1 Answer 1

2

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);)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for quick response. Changed code to as suggested in the original post , still facing same issue :(
@maheshreddy The answer is correct on that. You're doing the same thing as before. The function shouldn't be called with (fileName). You're having IIFE instead of $timeout callback, which stands for immediately invoked function expression.
aah, Thanks, I was trying differents things and made it a IIFE. Removing the IIFE solved, it, Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.