Few things confusing a newbie while writing JavaScript

O

Oltmans

Hi, I hope you're doing well. I'm not an expert JavaScript programmer
and have few questions that I hope you can answer. I will really
appreciate any help.

1- My JavaScript code basically provides the user the ability to
perform certain actions (let's call them A(), B() and C()) after
login(). All these actions are happening asynchronously via Ajax. So
after writing this code here is how my code looks like

login()
A()
B()
C()

Problem is that login() is happening asynchronously(using Ajax) so I
never know when login() is done working and A()/B()/C() must only
execute after successful login(). But in my case, code first goes to
login() and while login() is still working the code jumps to B() hence
it's not working as desired.

In a nutshell, I want to provide the user ability to code like this
login()
A()
B()
but A() should only execute once login() is done working. Any ideas on
how to circumvent this situation? Please enlighten me. I will really
appreciate all help.

Thanks!
 
A

Andreas Bergmaier

Oltmans said:
All these actions are happening asynchronously via Ajax.
[...]
Problem is that login() is happening asynchronously(using Ajax) so I
never know when login() is done working and A()/B()/C() must only
execute after successful login(). But in my case, code first goes to
login() and while login() is still working the code jumps to B() hence
it's not working as desired.

Read https://developer.mozilla.org/en/AJAX/Getting_Started or google for
some other docs, there are many out there, to see some example code.

You can
* send ajax requests synchronously, but then everything will stop
working until the response is there
* set an event listener for the response. This is done by assigning a
function to request.onreadystatechange which checks the current state
and, when ready, calls A, B and C or sends the next request.

Bergi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top