Take the 2-minute tour ×

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. 
share|improve this question
So function a is asynchronous? –  Musa Apr 16 '13 at 19:16
you can have a global variable and set it's value to some constant value in function a..and then check that value before calling function b –  Ankush Jain Apr 16 '13 at 19:17
look at this, this may help you. stackoverflow.com/questions/1370766/… –  jycr753 Apr 16 '13 at 19:18
yes function a is asynchronous –  nyshangal Apr 16 '13 at 19:18
Have "b" check to see if the number of stores is visible... –  Matthew Blancarte Apr 16 '13 at 19:31

3 Answers 3

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

share|improve this answer

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
share|improve this answer
the problem is i cannot update function foo(), so how do i add foo.complete=? inside function foo() :( –  nyshangal Apr 16 '13 at 19:23
Well JavaScript has a single execution flow, so when a function it is call it will execute, so what you can do it is just put some events call, such as setInterval, if this bool becomes true, them the first function it is done so will be the green line to execute the next function –  jycr753 Apr 16 '13 at 19:33

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');

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
share|improve this answer
OP can't do this: he hasn't got access to function a. Also localStorage isn't very X-browser friendly (older browsers don't support it) –  Elias Van Ootegem Apr 17 '13 at 9:53
You can use the jQuery.data() function to add custom data to your dom objects. Implementation is similar as the sample above, you can add data to your dom object like this: $("#div-response").data("myObject", "done"); To delete data you can use jQuery.removeData() like this: $("#div-response").removeData("myObject"); –  jherax Jun 17 '13 at 21:39
Why use jQuery, why not simply document.getElementById('div-response').setAttribute('data-myObject', 'done');? Honestly, there's nothing jQ does, that can't be done in vanilla JS –  Elias Van Ootegem Jun 18 '13 at 9:13
Thanks for your appreciation @EliasVanOotegem. I proposed jQuery because the question has jquery as tag. :) –  jherax Jun 18 '13 at 20:07

Your Answer


