2017-08-01 81 views
2

我在做一个密码生成器。试图执行通行短语。函数不会返回一个值

功能loadFile();不会返回一个值,以我的主要功能,generate();

我觉得我太近的问题,并不能逻辑出来。我可以使用一些帮助。谢谢。

测试地点:https://jf0.000webhostapp.com/passWordGenerator/ test.js来源:https://jf0.000webhostapp.com/passWordGenerator/test.js

源越来越那种长,对不起。

//Main generate password function 
function generatePassword(){ 
    var x = document.getElementById("passOutput"); 
    var p = document.getElementById("testP"); 
    x.value = generate(); 
    //Make sure they aren't using an insecure number of characters 
    checkMaxChars(); 
    p.innerText = x.value; 
} 

//Generate a password. 3 passSets for customization, one for passphrase 
function generate(){ 
    var nL = document.getElementById("noLetters"); 
    var nN = document.getElementById("noNumbers"); 
    var nS = document.getElementById("noSymbols"); 
    var pPK = document.getElementById("passWordPhrase"); 
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
    var numbers = ""; 
    var symbols = "[email protected]#$%^&*()_+~`|}{[]:;?,.-='"; 
    var maxChars = document.getElementById("maxCharControl").value; 
    var generatedPass = ""; 
    var holdPass = ""; 
    //Main loop 
    for(var i = 0;i < maxChars;i++){ 
     //Pick random value for each passSet 
     var passSet1 = chars.charAt(Math.random() * chars.length); 
     var passSet2 = numbers.charAt(Math.random() * numbers.length); 
     var passSet3 = symbols.charAt(Math.random() * symbols.length); 
     //if a checkbox is selected, clear out that value 
     if(nL.checked == true){passSet1 = "";} 
     if(nN.checked == true){passSet2 = "";} 
     if(nS.checked == true){passSet3 = "";} 
     //Randomly select a set to be added to holdPass, or not if it is empty. 
     var r = Math.floor((Math.random() * 4) + 1); 
     if(r == 1){if (passSet1 == ""){i--}else{holdPass += passSet1;}} 
     if(r == 2){if (passSet2 == ""){i--}else{holdPass += passSet2;}} 
     if(r == 3){if (passSet3 == ""){i--}else{holdPass += passSet3;}} 
     if(r == 4){ 
     if(pPK.checked == true){ 
      //get the value from loadFile(); and apply it to passSet4, add that to holdPass. 
     }} 
    } 
    generatedPass = holdPass; 
    console.log("Max Characters:" + maxChars); 
    console.log("passSet1:" + passSet1); 
    console.log("passSet2:" + passSet2); 
    console.log("passSet3:" + passSet3); 
    console.log("Iteration:" + i); 
    console.log("Random Position:" + r); 
    console.log("Password:" + holdPass + "::::" + holdPass.length); 
    //return password 
    return generatedPass; 
} 

//Make sure they didn't select all the checkboxes 
function checkBoxes(){ 
    var nL = document.getElementById("noLetters"); 
    var nN = document.getElementById("noNumbers"); 
    var nS = document.getElementById("noSymbols"); 
    var pP = document.getElementById("passWordPhrase"); 
    var nA = document.getElementById("noticeArea"); 
    var nT = document.getElementById("noticeAreaText"); 
    if(nL.checked == true && nN.checked == true && nS.checked == true){ 
     nL.checked = false; 
     nN.checked = false; 
     nS.checked = false; 
     nA.style.display = "block"; 
     nT.innerHTML = "You cannot select all checkboxes at once."; 
     window.setTimeout(hideNotice,5000); 
    } 
    else{ 
     nA.style.display = "none"; 
     nT.innerHTML = ""; 
    } 
} 
//make sure the max characters is greater than 8 
function checkMaxChars(){ 
    var maxChars = document.getElementById("maxCharControl").value; 
    var nA = document.getElementById("noticeArea"); 
    var nT = document.getElementById("noticeAreaText"); 
    var x = document.getElementById("passOutput"); 
    console.log(maxChars); 
    if (maxChars < 8){ 
    x.value = ""; 
    nA.style.display = "block"; 
    nT.innerHTML = "You cannot generate a password less than 8 characters in length for security reasons."; 
    window.setTimeout(hideNotice,7000); 
    } 
} 
//hides the notice area div once the message is completed 
function hideNotice(){ 
    var nA = document.getElementById("noticeArea"); 
    var nT = document.getElementById("noticeAreaText"); 
    nA.style.display = "none"; 
    nT.innerHTML = ""; 
} 
//Load file 
function loadFile() { 
    var xhttp = new XMLHttpRequest(); 
    var x; 
    xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     x = this.responseText; 
     parseResponse(x); 
     } 
    } 
    xhttp.open("GET", "wordList.csv", true); 
    xhttp.send(); 
    return x; 
} 
//Format response 
function parseResponse(x){ 
    console.log("MADE IT HERE!"); 
    var dS,sV1,rPos; 
    dS = x.split(","); 
    for(var i =0;i < dS.length;i++){ 
     sV1 = dS[i]; 
    } 
    x = sV1; 
} 
+0

您正试图在AJAX请求完成之前返回'x'。您需要将该行移入您的AJAX成功处理程序。 –

+0

斯科特说,你应该在运行回调之前等待回应。我建议使用一个像axios这样的抓取库。 –

+0

@BradyEdgar为什么你会推荐一个第三方库?只需处理成功回调的结果即可。 –

回答

1

AJAX请求的结果将永远不可用,直到启动它的函数完成为止。在发起请求的函数结束之前,您正试图返回x,这是在AJAX请求完成之前。

您需要将该行移动到您的AJAX成功处理程序中。

function loadFile() { 
    var xhttp = new XMLHttpRequest(); 
    var x; 
    // The onreadystatechange callback function will execute 
    // at some future point after loadFile has completed, so 
    // you can only gain access to the AJAX result from within 
    // that function. 
    xhttp.onreadystatechange = function() { 
     if (this.readyState == 4 && this.status == 200) { 
     x = this.responseText; 
     parseResponse(x); 
     return x; 
     } 
    } 
    xhttp.open("GET", "wordList.csv", true); 
    xhttp.send(); 
} 
+0

谢谢你的帮助。添加后,实际调用loadFile();做它的'事情,它填充我的输出与“未定义”混合在一起。似乎我可以使用loadFile();将值传回给generate()函数,并将该值分配给holdPass,holdPass携带构建密码时使用。 所以这是好消息/坏消息。 再次感谢您的帮助! – johnfuller001

0

你xhttp请求与truexhttp.open("GET", "wordList.csv", true);前的值赋给它,因此,返回x异步方式打开。

编辑:表示同步而不是异步,因为我很愚蠢