Ajax call without returnFunction

I read from the help on Ajax Description as follows.


returnFunction , if supplied, is a function to be called when the results return, allowing asynchronous execution. If this parameter is not supplied, execution will continue at the next statement after the results return. Do not put MsgBox statements inside your Ajax code: they mess up the event sequence.

I am not using the returnFunction on my Ajax call. The app will run the next statement without waiting for the results return. Is it normal? Or I should put the next statement onto the returnFunction?

Can you include a code snippet?

With or without a callback, the underlying code is asynchronous. What that means is that, unlike procedural code where one statement is executed and then the next, in asynchronous code there are functions that get “dispatched.” What that means is that you call the function (ajax) and it starts the call and then immediately returns and your code continues to execute.

What the callback does, is when that ajax call completes, your callback code gets called and you get the results of the ajax call (either the data or the failure).

If you’re dependent on the results of the call before proceeding to the next step, you have to rethink how you put your user experience together. For instance, if you’re creating a signup form, you disable all the fields except the one the user is entering data for and in the callback code you enable the next field and move the focus to that field.

Does that make sense and help any?

Are you writing in Javascript or Basic? In Javascript, the “callback” parameter is the best way to go. I had issues with leaving the parameter out. It may have been I was a newbee when I tried all this.

If you’re concerned that the user will press some button, etc., while the response is coming back, then activate a modal over the screen until the Ajax completes.

In basic, without the “callback” parameter it works as expected. I just checked one of my first projects in AppStudio in Basic that did an ajax call. It doesn’t use the “callback”, but just waits till it’s done. Basic in AppStudio does a lot to make things easier, but you loose flexibility.

And you can insert Javascript into a basic code section :slight_smile:

I use the ‘callback’ to make sure the data returned from Ajax, then go to next step. It work fine now, but I need to create a lot of functions.

Yes, the code can get quite ugly. I suggest some sort of strategy that reuses code. I also use queues and stacks to control the flow of the program. Of course, my app lends itself to those sorts of hierarchical structures.

Now you know why everyone is not writing Javascript programs. :cowboy_hat_face:

Another method is to use a promise on the Ajax call. I’ve not done it that way, but it may lend a different approach to you code structure.

They call it “callback(hell)” and for good reason. I have one set of server “gets” nested 4 deep. Like Gary said, you need to really think through your strategy.

Then add on top of that the feature of background “syncing”: the user is doing one thing and you have a whole series of other events running to handle the ajax calls in the background. Like having two threads running. This is where a queue makes sense. Then you can handle one call at a time.

Remember, recursion can be your friend. There is no reason why you can’t do something like:

doAjaxSend(first record in queue);

function doAjaxSend(xxx) {
  ajax(xxx, xxx, AjaxDone);

function AjaxDone() { 
  if(no more records) { return; };
  doAjaxSend(next record from queue);

Something else to think about is the wait time if the server is slow to respond. It could take 30 seconds or more at times. So the following code limits this to 10 seconds (set to what you want):

Function Syncing()
  DoingSync = True
  AjaxWaitTime = Now
  TS = SetInterval(TestAjaxDone,1000) 
End Function

Function TestAjaxDone()
  If DateDiff("s", AjaxWaitTime, Now) > 10 Then '10 seconds before bale out
      DoingSync = False
      M = "Unable to connect to the server at this time, please try again later."
      Call ShowMsg(M,0, "No Connect") 'custom message box
      'other clean up stuff here to reset the form calling the sync
      Exit Function
    End If 
End Function