简体   繁体   中英

Calling java method in Javascript loop function

I am running a javascript function which is under a for loop. The called javascript function calls a java method with the passed parameters. I don't know why java method not execute each time javascript function in loop is called. But it executes after loop has completed. So, called java method doesn't follow the order of passed parameter. The sample code of my program will look like this:

function firstfunction() {
    var x = 5;
    for (var j = 0; j < 5; j++) {
        secondfunction(j, x);
    }
}

function secondfunction(j, x) {
    JavaClass.Javamethod(j, x, callback);
}

Java method do some calculation and return some value as callback. I want my code to call the java method and return the value and then go in javascript loop to take the next parameter and so on.

You can not directly call the Java method/function once JavaScript code is executed, to execute that you have to use JavaScript ajax function which is used to execute backend programming language function for eg. Java method based on the url you defined of that method and passed the parameters in the ajax 'data' which is required by the java method and in ajax response you will get the return value of that java method.

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