setTimeout in Lightning Component [AURA]

2,136 Views

I found it very important as many places, JS functions can save your a**. I mean LIFE. But specially setTimeout function when you need to perform some function after specific time.

Here is an example to use JS setTimeout function in AURA Component,

1. If set timeout modifies any attributes, use $A.callback-

window.setTimeout(
     $A.getCallback(function() {
          helper.hideSpinner(component, helper)
     }), 500
);

2. As per setTimeout documentation, use any one of the following if calling function doesn’t modifies any attributes:

window.setTimeout(function(){ helper.hideSpinner(component, helper)}, 500);
// or
window.setTimeout(helper.hideSpinner, 500, component, helper);

Sources,

https://github.com/edelrabe/Salesforce-Recipes/wiki/setTimeout-in-Lightning-Component
https://salesforce.stackexchange.com/questions/206017/lightning-component-error-for-settimeout-bind-must-be-called-on-a-function/206018

Leave a Comment