Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

function execute completion?

Hi,

In javascript how do I insure that one function finishes executing before the next function executes?  For example:

function foo() {

document.form[0].submit();

}

function foo2() {

...code

}

function foo3() {

foo();
foo2();

}

Thank you.
0
calypsogurl00
Asked:
calypsogurl00
  • 5
  • 4
  • 2
  • +4
1 Solution
 
AntithesisCommented:
If I'm not mistaken, the submit() command will exit the page before you can get to foo2().  What would be in foo2() that you would need it to execute after submitting a form?
0
 
cubrovicCommented:
Antithesis is write about this.
Otherwise
js functions run at the same thread so this means that first functions must end and then other function starts executed.

Exception to this is when you using timer
   window.setTimer("MyFunction()","20")
0
 
mattjp88Commented:
you can just do something like this:

function foo() {

document.form[0].submit();

foo2();

}

function foo2() {

...code

}

matt :-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jonnalCommented:
mmmm dont know about that mattjp88

think you need the function in a different frame than the target of the form submit ....

function foo() {// not in top.frames[1]

top.frames[1].document.form[0].submit();

foo2();

}

function foo2() {

...code

}




0
 
mattjp88Commented:
oh i missunderstood the question.  so what you want to do is exicute the 2ed function after the form is submitted.  you could just open a little popup window and then colse it when the next page loads.

matt :-)
0
 
calypsogurl00Author Commented:
Basically...I need to submit the form then redirect the page's location...but if I do forms[0].submit() then window.location=index.html...the page redirects to index.html before the form submits...I have to ensure that the form is submitted before the page is redirected...
0
 
whammyCommented:
If you want to be sure, have foo1 and foo2 return a boolean value (true or false) and only execute foo3 if the value is true. Make sure you are expecting a return value from the functions that you reference. ;-)

Hope that guides you in the right direction. It should!!!
0
 
whammyCommented:
P.S. If you are not sure about boolean values (true or false), please post again, this is a very important programming concept, and it will make your life much easier. ;-)
0
 
whammyCommented:
Hmm.. although rereading your above post:

Basically...I need to submit the form then redirect the page's location...but if I do forms[0].submit() then window.location=index.html...the page redirects to index.html before the form submits...I have to ensure that the form is submitted before the page is redirected

It looks like you need to submit the form before you redirect - you might try using setTimeout() in javascript - although I can't guarantee you good results, as this kind of logic would me much more reliable if you redirected server-side.
0
 
calypsogurl00Author Commented:
Actually...I tried:

function foo() {

document.form[0].submit();
return true

}

function foo2() {

...code

}

function foo3() {

fooflag = foo();

if (fooflag == TRUE) foo2();

}

but...TRUE was returned before the form was submitted...
0
 
whammyCommented:
Ok, so you need to encapsulate your logic before you submit the form, if there are multiple conditions that preclude form submission. ;-)
0
 
calypsogurl00Author Commented:
whammy...

what do you mean..encapsulate the logic before the submit form?
0
 
calypsogurl00Author Commented:
whammy...

what do you mean..encapsulate the logic before the submit form?
0
 
jonnalCommented:
still thing you need a frameset

dose foo3 act on the returned form page
0
 
whammyCommented:
Strangely enough, I'm not sure what I meant, exactly... do you have access to the script that processes the form?

It would be much simpler to simply redirect to a thank you page after processing the form server-side.
0
 
jaysolomonCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: whammy {http:#8199707}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now