Javascript布尔结果失败

Javascript boolean results failing

本文关键字:失败 结果 布尔 Javascript      更新时间:2023-09-26

为什么这个变量(lanFound)变成未定义?

得到以下输出:

灯泡一刻!:)

当我输入的时候,输出的序列泄露了它!Ajax是异步的,所以true会在代码继续后返回!不管怎样我都会贴出来,可能对某些人有用!

  • 测试:DK
  • result is: undefined
  • /sites/cspdKnowledgeAssemblyPlatform/ApprovedContent/DKCover Letter.docx succeeded

我有一组docx文件,但正在添加对语言的支持,但为了测试已添加的文件(docx),我使用以下代码(OK,这是一个长期的变体,允许我调试):

    fileUrl = filePath + fileName;
        if (lan != "EN"){
            showNotification("testing for: " + lan);
            var lanFound = false;
                lanFound = checkURL(filePath + lan + fileName);
                showNotification("result is: " + lanFound);
            if(lanFound){
                debugger;
                fileUrl = filePath + lan + fileName;
                showNotification("found " + fileUrl); 
            }
        }
        function checkURL(urlFileName){
            $.get(urlFileName)
            .fail(function() {
                showNotification(urlFileName + " failed");
                return false;
            })
            .done (function() {
                 showNotification(urlFileName + " succeeded");
                return true;
            });
        }

你可以忽略这个-只是为"showNotification"上下文添加的)

        function showNotification(content){
             var currentText =  $( "#resultpanel" ).html();
             currentText = currentText + "<br/>" + content;
             $( "#resultpanel" ).html(currentText);  
        }

您不能以这种方式调用ajax调用,代码将在返回结果之前通过,由于它是异步的,结果作为变量在代码传递它时被读取为未定义。

对不起,当我输入问题时,我意识到答案,但无论如何张贴,因为这可能对某人很方便。