5

I want to make recursive call in js controller on my lightning component.. Following is my js controller but I am getting an error as

'callback' must be a valid Function

check : function(component, event, helper) {
    var str = component.get('v.message');
    if(str == null){
        str = '';
    }
    $A.getCallback(helper.check(component,str),1200);
}

ControllerHelper Code :

check : function(cmp,a) {
    var msg = 'Welcome to Dairy Startup';
    if(a.length < msg.length){
        a = msg.substring(0,a.length + 1);
        cmp.set('v.message',a);
    }
}

How Can I make recursive call to helper method?

4

You're actually calling the function, but instead what you want to do is bind it to get a function. That looks like this:

$A.getCallback(helper.check.bind(helper,component,str));

Where the first parameter is the "this" variable (the helper), and the parameters thereafter are function parameters.

You can read more about binding over on MDN.

You also are presumably trying to create a delay, so you need to use setTimeout:

setTimeout($A.getCallback(helper.check.bind(helper,component,str)), 1200);
4
  • Thank You very much..That was very well explained.. Can you please tell me how to recursively call the above method?I tried setInterval($A.getCallback(helper.check.bind(helper,component,str)), 1200)) but it is not working – Akki Gupta Nov 9 '18 at 15:14
  • @AkkiGupta You're binding the value "str", so if you're trying to basically do an animation, move the part where you get the v.message attribute to the check function. – sfdcfox Nov 9 '18 at 15:18
  • 1
    @sfdcfox Is this called recursion? I thought “recursion” means function calling itself. I have not studied computer science in college, so I could be wrong and want to confirm. – javanoob Nov 10 '18 at 2:42
  • 1
    @javanoob I didn't bother to correct the terminology, but you're right, a function calling itself is a recursive function. What the OP probably should have done is set the time out for the next call to check within check, thus making it a recursive function of sorts (though asynchronously). – sfdcfox Nov 10 '18 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.