Tutorial :Functions too fast? So they get skipped?



Question:

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!


Solution:1

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.


Solution:2

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();.


Solution:3

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »