简体   繁体   中英

How to perform the second callback after the completion first one in javascript?

I have googled a lot of sites to get a way to do sequential callbacks that is the second function should execute only after the completion of first function. In my case i am updating some properties of the nodes in button click and each property updation should perform a single callback. My problem is that some of property updates are based on the previous properties and i cant achieve this ..since the javascript execute all in one.

So please point me out in the correct direction..how to perform the callbacks after the completion previous call backs.

Thanks in advance.

Here is my code :

function SetProperties() {
        var nodes = Control1.GetNodesList();
        var ColorStyle = {
            Color: 'Red',
            ForeColor: 'Maroon',
            ColorAlphaFactor: '255',
            ForeColorAlphaFactor: '255',
            Type: '2',                
            PathBrushStyle: '1'
        };

         var LineStyle = {                
            LineColor: 'Blue',
            LineWidth: '2'
         };

         Control1.SetColorStyle(nodes[0].FullName, ColorStyle);
         Control1.SetLocation(nodes[0], 150, 300);
         Control1.SetRadius(nodes[0], 20);
         Control1.SetShading(nodes[0], true);
         Control1.SetSize(nodes[0], 150, 150);
         Control1.SetLabel(nodes[0], label);            
         Control1.SetLineStyle(nodes[0], LineStyle);
    }

If understand, you could define hidden field in your page

<input type='hidden' runat='server' id='funToCall'/>

then you could create switch in your javascript

var funToCall=<% funToCall.Value %>;
switch(funToCall){
   case "fun1": Control1.SetColorStyle(nodes[0].FullName, ColorStyle); break;
   case "fun2": Control1.SetLocation(nodes[0], 150, 300); break;
...
}

and in code behind define which is next function to call in every postback

funToCall.Value="next fun...";

It depends on how your first callback function works. The JavaScript doesn't actually execute all at once, it executes sequentially in a single thread. If your first callback does all its work in one series of commands then the next piece of code will only start executing once this is done.

For example:

var a = 0;

function fun1() {
    a = 1;
}

function fun2() {
    alert(a);
}

fun1();
fun2();

Will give the same result as:

var a = 0;
a = 1;
alert(a);

Because fun2 will be executed after fun1 has finished executing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM