?
Solved

function execute completion?

Posted on 2003-03-24
17
Medium Priority
?
282 Views
Last Modified: 2007-12-19
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
Comment
Question by:calypsogurl00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +4
17 Comments
 
LVL 1

Expert Comment

by:Antithesis
ID: 8198450
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
 
LVL 7

Expert Comment

by:cubrovic
ID: 8198625
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
 
LVL 6

Expert Comment

by:mattjp88
ID: 8198716
you can just do something like this:

function foo() {

document.form[0].submit();

foo2();

}

function foo2() {

...code

}

matt :-)
0
Industry Leaders: 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!

 
LVL 4

Expert Comment

by:jonnal
ID: 8198945
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
 
LVL 6

Expert Comment

by:mattjp88
ID: 8199012
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
 

Author Comment

by:calypsogurl00
ID: 8199687
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
 
LVL 4

Expert Comment

by:whammy
ID: 8199690
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
 
LVL 4

Expert Comment

by:whammy
ID: 8199695
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
 
LVL 4

Accepted Solution

by:
whammy earned 500 total points
ID: 8199707
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
 

Author Comment

by:calypsogurl00
ID: 8199714
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
 
LVL 4

Expert Comment

by:whammy
ID: 8199719
Ok, so you need to encapsulate your logic before you submit the form, if there are multiple conditions that preclude form submission. ;-)
0
 

Author Comment

by:calypsogurl00
ID: 8199769
whammy...

what do you mean..encapsulate the logic before the submit form?
0
 

Author Comment

by:calypsogurl00
ID: 8199779
whammy...

what do you mean..encapsulate the logic before the submit form?
0
 
LVL 4

Expert Comment

by:jonnal
ID: 8205536
still thing you need a frameset

dose foo3 act on the returned form page
0
 
LVL 4

Expert Comment

by:whammy
ID: 8206910
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
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10149728
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question