1

I tried a lot to call another helper method from a helper method using setinterval

 handleAsynchronousLoad:function(component, activityFilter, dateFilter, selectedTalentGroupFilter,currentLoad){
   window.setInterval(
        $A.getCallback(function() {
            var a = '0000';
            console.log('in async');
            helper.check(a);

        }), 5000
    );     
},
check : function(b){
    console.log('hey');
}

The check method is not being called. This both methods are in helper.

In each 5 seconds 'in async is printing'

Please let me know what am i doing wrong.

2
  • use this.check(a); to call helper method from same helper file Commented Sep 2, 2020 at 10:26
  • I tried with that as well.
    – NBR
    Commented Sep 2, 2020 at 10:36

3 Answers 3

4

When we want to call another helper method from one helper method the standard way is to use 'this' keyword.

But it doesn't work all the time as 'this' keyword looses context when a callback function is involved.

In your scenario it will be good if you store 'this' in a variable and use that variable to call the other helper method. Try with below code.

handleAsynchronousLoad : function(component, activityFilter, dateFilter, selectedTalentGroupFilter, currentLoad){
    let self = this;
    window.setInterval(
        $A.getCallback(function() {
            var a = '0000';
            console.log('in async');
            self.check(a);

        }), 5000
    );     
},
check : function(b){
    console.log('hey');
}
1
  • This works great!!!Thanks
    – NBR
    Commented Sep 2, 2020 at 12:39
3

Update your code snippet as shown below:

handleAsynchronousLoad:function(component, activityFilter, dateFilter, selectedTalentGroupFilter,currentLoad){
   var checkFn = this.check; 
   window.setInterval(
        $A.getCallback(function() {
            var a = '0000';
            console.log('in async');
            checkFn(a);
        }), 5000
    );
}
3

Using an arrow function preserves "this" automatically:

window.setInterval(
    $A.getCallback(() => {
        var a = '0000';
        console.log('in async');
        this.check(a);
    }), 5000
);     

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .