views:

147

answers:

4

Hi all,

With a function a div-popover gets called and filled with dynamic data using Ajax, PHP, MySQL and some HTML/CSS. All goes fine.

When I want to delete an entry in the list just popped over it functions as it should. When I send an update request for my list it also goes the way I want it. But, when i call delete(); update(); right after eachother my first function gets skipped somehow.

When I place alert()'s in both functions I see both functions are getting executed and the scripts walk fine through my ajax function, PHP ajax handler and returns the result back to the user, and with the alerts on all is going well too!

So my question is, are my functions too fast? Or is there something I'm missing here which is causing the non-delete?

Solution I've moved the update call to the line after the xmlHttp.resonseText in the delete function. In that way the second function call gets executed after the first function is done. Thanks all!

+3  A: 

It sounds like the two methods are executed at the same time (asynchronously) since its AJAX.

you want them to be excuted synchronously.

See this patterns page for more information... Ajax patterns

jeff porter
Thanks for your reply. Helpful resource. As I said to the others, my votes for today are all used. Tomorrow I'll give you credits! Thanks again!
Ben Fransen
+3  A: 

javascript will just execute the next statement while an ajax call is going on. Most ways of using ajax have an on complete function that you can call, so that code that you want executed after an ajax call is called only afterwards.

I've not worked with php, but it may be worth looking into that.

Jimmeh
You are right. i've placed the update function after the xmlHttp.response, that way they get executed after eachother instead of at the same time.
Ben Fransen
+4  A: 

My guess would be that you haven't thought about the A in AJAX. It stands for asynchronous. That means that when you perform an XmlHttpRequest call, it will be executed in the background. I.e. after you've called delete(); the script will immediately continue and execute update();.

Emil H
Asynchronous, JavaScript, And XML. :-)I'll upvote you for a more descriptive answer than mine.
jeff porter
I was only talking about the A. :)
Emil H
You're correct, that was the problem. Its solved now. My votes for today are all used, tomorrow I'll vote for your answer.
Ben Fransen
A: 

http://ejohn.org/blog/how-javascript-timers-work/ gives useful insight into asynchronous JavaScript execution.

George V. Reilly