PDA

View Full Version : Wait for current function response.



rhytha268
26 Oct 2012, 11:09 PM
Hi,

I have a code in which i'm calling a function and that function having Ajax request so it's taking a bit time to execute .. but at same time my next line code executed.. i want to stop next line code execution until my first line code is executing ...

here is my code -

39613

39614

Here i want to alert 's' first then alert 'd'

Any Idea ?

rhytha268
27 Oct 2012, 1:22 AM
Okay i found answer my self..

I used -

RWM.util.AjaxRequest.getPost(val);
setTimeout(function(){alert('d');}, 500);

It work for me.

siebmanb
27 Oct 2012, 2:13 AM
It works because in your case 500ms is enough, but it may not be the case every time isn't it ?

I believe the easiest way is to put the code following the Ajax request inside the callback of the Ajax request itself.

For example if you want to do this, and have line 2 executed after the callback :



Line 1
Ajax request
Line 2


You should do this :



Line 1
Ext.Ajax.Request({
url: yourUrl,
success: function(response) {
Line 2
}
})

Animal
28 Oct 2012, 1:05 AM
Okay i found answer my self..

I used -

RWM.util.AjaxRequest.getPost(val);
setTimeout(function(){alert('d');}, 500);

It work for me.

You are heading for trouble in your project if you start thinking like this.

To develop in Javascript you must understand and work with asynchronous operations.

You have to decompose your app into classes, each of which have methods to perform their function.

One of these methods is going to take care of processing the result of that asynchronous Ajax call. The method which fires it off has no further responsibility after firing it off. Move the response handling code into another method.

rhytha268
28 Oct 2012, 11:38 PM
Hi siebmanb (http://www.sencha.com/forum/member.php?310564-siebmanb),

Thank you for you suggestion ..
But in my case i have ajax request in different class file .. so just i have some restriction so i can't use callback i know callback is good one in this case.

Tim Toady
31 Oct 2012, 7:09 AM
You need to change your class/function to allow you to add a callback then. Your solution only works by chance. You can't guarantee it will always take less than 500ms to come back.

siebmanb
31 Oct 2012, 8:01 AM
So you are telling me that your callback, in a class A, is supposed to call a function in a class B ? Is it really necessary ? If the function is only called by that callback, why not move it into class A ?

You should post some *simplified* code so we can understand what you are trying to do.

But never ever use a setTimeout to deal with this. Never >:)

Animal
31 Oct 2012, 9:34 AM
Hi siebmanb (http://www.sencha.com/forum/member.php?310564-siebmanb),

Thank you for you suggestion ..
But in my case i have ajax request in different class file .. so just i have some restriction so i can't use callback i know callback is good one in this case.

The .js file in which a method is coded makes no difference.

Just look at ExtJS. Several hundred class files. And Container.js uses methods which are in MixedCollection.js

I don't understand your point.