1

I am very new to Java script and need to use callback function in my java script function.I dont know how to use callback function. Below is my code:

function SelectedFeature() {

// Here is my code

call_Method1();

call_Method2();

}

The problem in the above function is that, call_method2() starts executing before call_Method1() ends its execution. To solve this problem, someone told me to use callback function. Now how can i use callback function in my SelectedFeature() function. Please explain by using code sample.

5
  • are you sure call_Method2() executes before call_Method1()? Commented Mar 10, 2011 at 18:04
  • 2
    What happens in Method1 and Method2? Are you using setTimeout? Commented Mar 10, 2011 at 18:05
  • call_Method2() cant run before call_Method1(), its only possible if you are making any asynchronous request in call_Method1(). Commented Mar 10, 2011 at 18:07
  • please tell waht are you doing in call_Method1()?? Commented Mar 10, 2011 at 18:08
  • uhhh, yeah, are you using some sort of asynchronous call like setTimeout,setInterval or ajax? Commented Mar 10, 2011 at 18:08

4 Answers 4

3

Here is a simple example:

function processData(data) {
    firstStep(data, secondStep);
}    

function firstStep(data, callback) {
    var result = getResult(data);
    if (result.success) {
        callback(result);
    }
}

function secondStep(data) {
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Dear Praveen Parsad, you are asolutly right. I am making asynchronous request in method1(). i need Method2() should be called after completing execution method1(). But in my case, method2() calls before method1()completes its execution. Now how can ifix this ?
1

If you are making an ajax call, to stop the execution of call_method2, in ajax request we can do something like,

$.ajax({
  url: "your url",
  type: "Get",
  success: call_Method2,
  failure: call_Method2
});

Now, call_Method2 will run only when call_Method1 is finished.

Comments

1

Here is the solution

 function SelectedFeature() {

    this.call_Method1 = function(data, callback) {
        var success = false; // define success flag with default false

        //your code
        //here write your logic and assign true of false to success based on your code execution
        success = true;

        if (success) {
            //pass data or add one more parametere to call_Method1 if different data
            //this.call_Method1 = function(data,data1,callback){
            //callbackdata1);
            callback(data);
        }
    }

    this.call_Method2 = function(data) {
        console.log(data);
    }

}

var feature = new SelectedFeature();
var data = "some data"
feature.call_Method1(data, feature.call_Method2);

Comments

0

You Can follow this callback structure

function SelectedFeature(call_Method1, call_Method2) {

    // Here is my code

    call_Method1();

    call_Method2();

}

SelectedFeature(function () {
    console.log('call_Method1')
}, function () {
    console.log('call_Method2')
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.