How to use the setTimeout function in the Salesforce Lightning Component?

1.1K    Asked by ChrisDyer in Salesforce , Asked on Jul 31, 2023
 I am a novice to the Salesforce Lightning Components and I want to understand how to deploy the settimeout function in my lightning component. The code is given below: ({ init : function (component, event, helper ) { helper. showSpinner (component) ; v record }
Answered by Darsh K

The setTimeout function refers to a native JavaScript function that creates a timer to execute the callback function, thereby calling the function on termination of the timer. You can use $A.getCallback() to wrap a code that alters a lightning component external to the usual re rendering lifecycle, including the setTimeout9) call. The $A.getCallback() call guarantees that the infrastructure rerenders the altered component and executes the enqueued tasks. The example where you can use $A.getCallback() is calling window.setTimeout9) in the event handler to process the logic after time delay. This places the code outside the infrastructure’s call stack.


The below example sets the attribute on the component to true after the time delay.

window.setTimeout( $A.getCallback(function() { cmp.set("v.visible", true); }), 5000 );
You can try the below-mentioned code:
action.setCallback(this, function(response){ var state = response.getState(); if (state === "SUCCESS"){ isValid = response.getReturnValue(); window.setTimeout( $A.getCallback(function() { alert(isValid); }), 5000 ); $A.get("e.force:closeQuickAction").fire(); $A.get('e.force:refreshView').fire(); } else{ console.log("Unknown error"); component.set("v.errorMessage", "Unknown error"); } helper.hideSpinner(component); });

The Salesforce Certification Trainingoffered at JanBask Training provides experience like offline classes to help candidates master the Salesforce Development and Administration in a better way to face the tough job market scenario. The course guarantees job success and lets you qualify the certification exam through intensive, and expert-led virtual sessions and hands-on practical assignments. The course will teach you to create and configure the salesforce account to help you gather, extract, and examine the data relevant to the customer base. You can also avail the Force.com platform to create and use the advanced cloud apps with utmost confidence.



Your Answer

Interviews

Parent Categories