Newer Version Available

This content describes an older version of this product. View Latest

Modifying Components Outside the Framework Lifecycle

Use $A.run() to wrap any code that modifies a component outside the normal rerendering lifecycle. The $A.run() call ensures that the framework rerenders the modified component and processes any enqueued actions.

You don't need to use $A.run() if your code is executed as part of the framework's call stack; for example, your code is handling an event or in the callback for a client-side controller action.

An example of where you need to use $A.run() is calling window.setTimeout() in an event handler to execute some logic after a time delay. This puts your code outside the framework's call stack.

This sample sets the visible attribute on a component to true after a five-second delay.

1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17window.setTimeout(function () {
18    $A.run(function() {
19        if (cmp.isValid()) {
20            cmp.set("v.visible", true);
21        }
22    });
23}, 5000);

Note how the code updating a component attribute is wrapped in $A.run(), which ensures that the framework rerenders the modified component.

Always add an isValid() check if you reference a component in asynchronous code, such as a timeout.

Note