nyshangal
nyshangal

Reputation: 102

Multiple functions on form submit jQuery

I have to call two functions on form submit but i want to make sure that that second function is executed only after the first function is executed. I do not have control over the first function, so i cannot edit it at all.

function a(et){
   //function a script
}

 function b(evt){

 //function b script
}

from function b is there a way that i can check if function a was completed or executed fully. 

Upvotes: 2

Views: 964

Answers (3)

jherax
jherax

Reputation: 5267

Yes, you can, there is a lot of ways to do that, but here is a simple way to make it.
You can use persistent object localStorage.
You can use session object sessionStorage.

function a(evt){
   // Asynchronous function
   // ... your code here
   // before finish function create the persistent object
   localStorage.setItem('myObject', 'done');
   return;
}

function b(evt){
   // Checks if a() function was completed
   var _done = localStorage.getItem('myObject');
   if(_done && _done == 'done') {
     // your code here: the a() function was completed
     localStorage.removeItem('myObject');
   }
}

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

if you have no access to a at all then you can't do what you want to do here. No way: JS creates a call object when a function is called, and auto-GC's it when the call is complete. Sadly, the actual mem-management is off limits, so you can't check that. other trickery involves your meddling with the code of function a, which you say you cant get at... so no, you can't do this.

Sorry for that, but it's as the FAQ says: you might not always get the answer you're hoping for, but that doesn't mean the answer you don't like isn't true... :-P


There is, however, a little bit of hope for you: if both function a and b are callbacks or handlers of a submit event, you could look into ways of queueing those calls. Given the jQuery tag .queue() suggests itself

Upvotes: 1

Jorge Y. C. Rodriguez
Jorge Y. C. Rodriguez

Reputation: 3449

According to Eli Grey you can tested like this:

function foo() {
  foo.complete = false;
  // your code here
  foo.complete = true;
}
foo.complete = false;

if (foo.complete) { // foo execution complete
  // your code here
}

Upvotes: 0

Related Questions