JavaScript

A5.executeThisThenThat Method

Syntax

A5.executeThisThenThat(function1, function2 [, functionN])

Arguments

function1functionstring

The first JavaScript function to execute. Can be specified as a string of JavaScript or the name of a function to call.

function2functionstring

The next JavaScript function to execute after function1 finishes execution. Can be specified as a string of JavaScript or the name of a function to call.

functionNfunctionstring

One or more additional JavaScript functions to execute, each specified as a separate argument. You can specify as many functions as you need. Can be specified as a string of JavaScript or the name of a function to call.

Description

Executes multiple Ajax Callbacks synchronously.

Discussion

Ajax callbacks are, by definition, asynchronous. That means that if you have two Javascript commands that both do Ajax callbacks, the second command will execute before the first command has completed (i.e. before the first command has received a response from the server) and in fact, the second command might complete before the first command.

The asynchronous nature of Ajax callbacks can cause problem in a Javascript script if you are expecting commands to run synchronously.

The A5.executeThisThenThat() function makes it extremely easy to make Javascript code run synchronously. For example, consider the following Javascript function that does three consecutive Ajax callbacks and then displays an alert:

function mycode() {
    {dialog.object}.ajaxCallback('','','myXbasicFunction1');
    {dialog.object}.ajaxCallback('','','myXbasicFunction2');
    {dialog.object}.ajaxCallback('','','myXbasicFunction2');
    alert('Callbacks are complete');
}

If you were to call this function from a button, the alert would display immediately - long before any of the ajax callbacks had completed, and the callbacks themselves would all fire at more or less the same time.

However, using the A5.executeThisThenThat() function we can easily convert the function to run synchronously. Here is how:

function f1() {
    {dialog.object}.ajaxCallback('','','myXbasicFunction1');
}

function f2() {
    {dialog.object}.ajaxCallback('','','myXbasicFunction2');
}

function f3() {
    alert('Callbacks are complete');
}

function mycode() {
    A5.executeThisThenThat(f1,f2,f3);
}

Notice that the code is split into separate functions and then the individual functions are run synchronously by passing them in as arguments to the A5.runThisThenThat() function.

If one of the individual functions made two or more Ajax callbacks, these callbacks would be asynchronous. For example, if f2() were change to:

function f2() {
    {dialog.object}.ajaxCallback('','','myXbasicFunction2');
    {dialog.object}.ajaxCallback('','','myXbasicFunction3');
}

Then the code in f2() would run after the code in f1() had completed, but the two callbacks in f2 would be run asynchronously.

The arguments passed into the A5.executeThisThenThat() function can either be functions or Javascript code (i.e. strings).

For example:

A5.executeThisThenThat('alert("one");','alert("two");');
The A5.executeThisThenThat() function is not designed for use in Working Preview.

Videos 

Synchronous Script Execution

Ajax callbacks execute 'asynchronously'. That means that if you have defined an Action Javascript script with a series of actions (such as 'open grid 1', 'open grid 2', etc.), the actions run in parallel, not sequentially. In many cases you will want the actions to run sequentially, and this video demonstrates how you can easily convert an Action Script from asynchronous execution to synchronous execution.

See Also