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 at called. This both methods are in helper.

In each 5 seconds 'in async is printing'

Please let me know what am i doing wrong.

  • use this.check(a); to call helper method from same helper file – salesforce Developer Sep 2 at 10:26
  • I tried with that as well. – NBR Sep 2 at 10:36
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');
}
| improve this answer | |
  • This works great!!!Thanks – NBR Sep 2 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
    );
}
| improve this answer | |
2

Using an arrow function preserves "this" automatically:

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

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.