Question

I am calling sub functions that are connected to if statements in my function. Though the sub functions return true or false, it doesn't work

   if (functionAAA()){
       alert("111111111");

       if (functionBBB()){
                      alert("2222222");

           if (functionCCC()){
                          alert("333333333333");



           }            
       }
   }

functionAAA, functionBBB, functionCCC return true. I was expecting to get these alerts but they don't work. what am I doing wrong?

I think the problem is the function side.. functionAAA definition is

function functionAAA(){
    var indexId = 1;

    var dataItem = datasourceAppList.at(indexId);
    var deviceKey = localStorage.getItem('LsDeviceKey');
    var baseUrl = localStorage.getItem('LsBaseUrl');    
    var errorMessageInfoText;
    var status

    $.ajax({

        url: baseUrl + '/test/cloud/FE/1.0/test.php',
        data: {
            PAR1: dataItem.par1,
            PAR2: dataItem.par2,
            PAR3: deviceKey,
            PAR4: dataItem.par3,
            PAR5: "5664456"
        },
        type: 'GET',
        crossDomain: true,
        dataType: 'jsonp',
        error: function () {
            showErrorDialog("Key Validation Failure");;
        }

    }).done(function (data) {

        $.each(data, function (k, v) {

           // alert("k  " + k + "  v  " + v);
            if (k == 'STATUS') {
                status = v;
            } else if (k == 'ERROR_MESSAGE') {

                errorMessageInfoText = v;
            }
        })


        if (status == 'TRUE') {
            alert("ok");
            return true;

        } else {
            return false;
        };

    });    

}

the other functions directly return true;

by the way, I receive the "ok" alert in the function

Était-ce utile?

La solution

You need to restructure your code. The anonymous function inside .done() is executed after functionAAA has already completed and returned undefined. AJAX calls are Asynchronous, after all.

$.ajax returns a promise, so one option is to use .then():

function functionAAA(){
    return $.ajax({
        url: 'http://jsfiddle.net/echo/jsonp/',
        dataType: 'jsonp',
    });
}

functionAAA().then(function() {
    alert("111111111");
    if (functionBBB()){
        alert("2222222");
        if (functionCCC()){
            alert("333333333333");
        }            
    }
});

see it in action on JSFiddle.

Autres conseils

Your function doesn't return anything. You can't do an asynchronous call and return the result — that's what it means to be asynchronous. If you want to do something after the ajax call completes, you'll need to give the function a callback and call it instead of attempting to return from the ajax handler.

The script below works for me. Can you please post your functionAAA, BBB, etc?

    <script>
    function functionAAA() {
        return true;
    }
    if(functionAAA()){
       alert("hi");
    }
    </script>

Add this and make your ajax call synchronous so it waits for the response, checks it and returns true/false accordingly. Right now it shouldn't return anything.

async: false

try...

 if (functionAAA()){
           alert("111111111");

           if (functionBBB()){
                          alert("2222222");

              else if (functionCCC()){
                              alert("333333333333");



               }            
           }
       }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top