如何在jquery中进行回调函数?

时间:2014-05-23 15:32:51

标签: javascript jquery

请告诉我如何在jquery中进行回调函数。实际上我有这两行。我学习代码逐行排除。但是当我使用这两行时。有时它会调用" saveIntestFile"在完成removeAllBlankOrNull函数之前。我需要" saveIntestFile"完成后调用" removeAllBlankOrNull"

var JsonObj =removeAllBlankOrNull(parentTestSuite);
           console.log(JsonObj);
             console.log(isValid);
        //    setTimeout(function(){
             saveIntestFile(JsonObj,isValid);



  function removeAllBlankOrNull(JsonObj) {
        $.each(JsonObj, function(key, value) {
            if (value === "" || value === null) {
                console.log('%s (%s) : DELETE', key, value);
                delete JsonObj[key];
            } else if (typeof(value) === "object") {
                console.log('%s (%o) : OBJECT', key, value);
                if(key=="commandList"){

                    for(var i=0;i<value.length;i++){
                        var cmdobj=value[i];

                        var map = object.generateInputRequirment();
                        for ( var commandKey in cmdobj) {
                            if (commandKey == "commandParameters") {
                                var commandParamObj = JSON.parse(JSON
                                    .stringify(cmdobj[commandKey]));
                                for ( var commandParamKey in commandParamObj) {
                                    var paramValue = commandParamObj[commandParamKey];
                                    if (map[commandParamKey].required == true) {
                                        if (isBlank(paramValue)) {
                                            isValid = false;
                                            $("#" + globalCommandKey).closest('div').css('backgroundColor','red');
                                            //$("#" + globalCommandKey).css({
                                              //  backgroundColor : 'red'
                                            //});
                                            break;
                                        }
                                    } else {
                                        if (isBlank(paramValue)) {
                                            delete commandParamObj[commandParamKey];
                                        }
                                    }
                                    isValid = true;
                                }
                            }
                        }
                    }

                }
                JsonObj[key] = removeAllBlankOrNull(value);
            }
        });
        return JsonObj;
    }

2 个答案:

答案 0 :(得分:0)

您可以将函数传递给removeAllBlankOrNull函数:

function removeAllBlankOrNull(JsonObj, callback) {
//In your $.each whenever you want to call back just call callback();
}

或者您可以查看延迟承诺,例如herehere

答案 1 :(得分:0)

您可以尝试使用简单的javascript进行回调:

showMessage('No Callback');

var processData = showMessage('Callback added', function () {
        add(1, 6);
    });

function add(num1, num2) {
     alert(num1 + num2);
}

function showMessage(msg, callback) {
    alert(msg);
    if (callback != undefined)
        callback();
}