简体   繁体   English

jQuery在后台运行,但不在前台运行

[英]jQuery runs in background, but not in foreground

I have this code fragment: 我有这个代码片段:

 if(!encryption_state){ if(cKey=="" || cKey==null){ cKey=getKey(aid); //here we trying to obtain key if(cKey!="" && cKey!=null && cKey!=undefined){ if(isJSON(jKey) && encryption_state){ var tjKey = JSON.parse(jKey); tjKey[aid] = cKey; jKey = JSON.stringify(tjKey); }else{ jKey = json.stringify({aid: cKey}); } encryption_state=true; } } if(!encryption_state){ if(cKey=="" || cKey==null){ cKey=rndstr(32); //generate string } var arr = {}; if(isJSON(jKey)) arr = JSON.parse(jKey); arr[aid] = cKey; jKey = JSON.stringify(arr); encryption_state = true; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

But when i call getKey(kaid) function: 但是当我调用getKey(kaid)函数时:

 function getKey(kaid){ $.ajax({ method: "POST", url: "/?mod=key&fnc=syncKey", data: { aid: kaid }, done: function(data) { var tret = (JSON.parse(data)['msg']); return tret; } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Browsers don't continue do function getKey(), they do next commands in parent function, i don't know why they ignore web server answer and don't let function return server response :( 浏览器不会继续执行getKey()函数,它们会在父函数中执行下一个命令,我不知道为什么他们会忽略Web服务器的答案并且不让函数返回服务器响应:(

in general, an ajax call is asynchronous. 通常,ajax调用是异步的。 That means, a sequence like 这意味着,类似

 var a = 0;

 a = getAwithAjaxFromServer(...);

 console.log(a);

will immediately print "0" while the ajax is still runnng. 在ajax仍在运行时将立即打印“ 0”。

Your entire logic with cley and encryption_state has to be put into the done function: 您具有cleyencryption_state整个逻辑必须放入done函数中:

if(!encryption_state){
    if(cKey=="" || cKey==null){
        cKey=getKey(aid);
    }
}

and in your ajax: 在你的ajax中:

function getKey(kaid){
    $.ajax({
        method: "POST",
        url: "/?mod=key&fnc=syncKey",
        data: {
            aid: kaid
        },
        done: function(data) {
            var tret = (JSON.parse(data)['msg']);


            .... PUT ALL THE LOGIC HERE .....

        }
    });
}

You must understand asynchronous mechanism in javascript to continue calling ajax. 您必须了解javascript中的异步机制才能继续调用ajax。 There are a lot of resources and stackoverflow questions. 有很多资源和stackoverflow问题。 For example: https://www.pluralsight.com/guides/front-end-javascript/introduction-to-asynchronous-javascript 例如: https : //www.pluralsight.com/guides/front-end-javascript/introduction-to-asynchronous-javascript

So, you can convert the code so: 因此,您可以这样转换代码:

if(!encryption_state){
    var serverKeyCallback = function(cKey) {
        if(cKey!="" && cKey!=null && cKey!=undefined){
            if(isJSON(jKey) && encryption_state){
                var tjKey = JSON.parse(jKey);
                tjKey[aid] = cKey;
                jKey = JSON.stringify(tjKey);
            }else{
                jKey = json.stringify({aid: cKey});
            }
            encryption_state=true;
        }
    };
    var localKeyCallback = function(cKey) {
        if(!encryption_state){
            if(cKey=="" || cKey==null){
                cKey=rndstr(32); //generate string
            }
            var arr = {};
            if(isJSON(jKey)) arr = JSON.parse(jKey);
            arr[aid] = cKey;
            jKey = JSON.stringify(arr);
            encryption_state = true;
        }
    }
    manageKey(cKey, aid, serverKeyCallback, localKeyCallback);
}

function manageKey(cKey, kaid, serverKeyCallback, localKeyCallback) {
    if(cKey=="" || cKey==null) {
        $.ajax({
            method: "POST",
            url: "/?mod=key&fnc=syncKey",
            data: {
                aid: kaid
            },
            done: function(data) {
                var tret = (JSON.parse(data)['msg']);
                serverKeyCallback(tret);
                localKeyCallback(tret);
            }
        });     
    }
    else {
        localKeyCallback(cKey);
    }
}

Defining two encapsulated pieces of code, one to execute after serverResponse, and the other to execute after the serverResponse or when you have the cKey locally stored. 定义两个封装的代码,一个在serverResponse之后执行,另一个在serverResponse之后或本地存储cKey时执行。 I haven't tested the code, but it must work as you expect. 我尚未测试代码,但它必须能够按您期望的那样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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