同步VBscript更改为不正确的异步Javascript

同步VBscript更改为不正确的异步Javascript,javascript,vbscript,Javascript,Vbscript,因此,我的老板让我调查一个问题,他们正在将旧的VBscript脚本移植到Javascript脚本中,但Javascript的程序流不正确。 唯一的问题是我对这两方面都一无所知,但是如果你的老板问你,那么你必须制定一个计划 在VBscript中,用户可以选择,如果选择“否”,则不会显示下一个屏幕/视图。 但是,使用Javascript,无论用户是否选择“否/取消”,都会加载下一个屏幕/视图。这不应该发生 这是VBscript,重要的部分是//: vEffectiveDate = ScreenFo

因此,我的老板让我调查一个问题,他们正在将旧的VBscript脚本移植到Javascript脚本中,但Javascript的程序流不正确。 唯一的问题是我对这两方面都一无所知,但是如果你的老板问你,那么你必须制定一个计划

在VBscript中,用户可以选择,如果选择“否”,则不会显示下一个屏幕/视图。 但是,使用Javascript,无论用户是否选择“否/取消”,都会加载下一个屏幕/视图。这不应该发生

这是VBscript,重要的部分是//:

vEffectiveDate  = ScreenForm("smoke.Effective Date").Value
vLastRunDate    = ScreenForm("smoke.Last Run Date").Value
sStatus         = ScreenForm("smoke.Calc Status").Value

vMsg = ""

If Len(sStatus) = 0 Then
    sStatus = "SUCCESFUL"
    ScreenForm("smoke.Calc Status").Value = sStatus
End If

If Len(vEffectiveDate) > 0 Then

    If Not IsDate(vEffectiveDate) Then 
        vMsg = vMsg & "[Effective Date] Is not a date." & Chr(13)
    
    ElseIf cdate(vEffectiveDate) <= cdate(vLastRunDate) Then
        vMsg = vMsg & "[Effective Date] cannot be on/before " & vLastRunDate &"." & Chr(13)
    End IF

End If

//////////////////////////////////////////////////////////////////////////////
If UCASE(sStatus) <> "SUCCESFUL" Then
    sResponse = MsgBox ("Forecast calculation still busy. Results might not be accurate. Continue?", (vbYesNo), "WARNING")
    If sResponse = vbNo Then
        MsgBox cstr("Screen will refresh. Please click on Update to try again."), (vbOKOnly), "INFORMATION"
        ScreenForm("smoke.Calc Status").Value = "REFRESH"
        'msErr = "ABORT"
    End If
End If
//////////////////////////////////////////////////////////////////////////////

If vMsg <> "" Then
MsgBox (vMsg)
msErr = "ERROR"
End If
 var vEffectiveDate="";
var vLastRunDate="";
var sStatus="";
var vMsg="";
var sResponse="";
vEffectiveDate = document.getElementById("Smoke.Effective Date").value;
vLastRunDate = document.getElementById("Smoke.Last Run Date").value;
sStatus  = document.getElementById("Smoke.Calc Status").value;
vMsg = "";
if ((sStatus).length == 0  ){
    sStatus = "SUCCESFUL";
    document.getElementById("Smoke.Calc Status").value= sStatus;
}
if ((vEffectiveDate).length > 0  ){
    if (!isDate(vEffectiveDate)  ){
        vMsg = vMsg+"[Effective Date] Is not a date." + ";\r\n";
    } else if (  moment( toDate(vEffectiveDate)).isBefore(toDate(vLastRunDate)) ){
        vMsg = vMsg+"[Effective Date] cannot be on/before "+vLastRunDate+"." + ";\r\n";
    }
}
///////////////////////////////////////////////////////////
if ((sStatus).toUpperCase() != "SUCCESFUL"  ){
 $.confirm({title:  "Confirmation",columnClass: 'col-md-6 col-md-offset-3', content:"Forecast calculation still busy. Results might not be accurate. Continue?",
buttons: {confirm: function() { sResponse= 1;},
cancel: function() {sResponse= 2;return;}}});
if (sResponse == 2  ){
        $.alert({title:  "INFORMATION",columnClass: 'col-md-6 col-md-offset-3', content:("Screen will refresh. Please click on Update to try again.").toString(),});
        document.getElementById("Smoke.Calc Status").value= "REFRESH";
  msErr = "ABORT";
    }   
}
//////////////////////////////////////////////////////////


if (vMsg != ""  ){
$.alert({title: 'Validation Message',columnClass: 'col-md-6 col-md-offset-3', content:(vMsg),});
msErr = "ERROR";
}
所以,我看到Javascript中有一个异步等待承诺的概念,但似乎是这样,我们需要记住,我们的一些用户实际上仍然使用IE…唉。。。 因此,我似乎无法使用异步等待概念,但我不确定在这种情况下是否有必要使用它

然后,我也在这里读到了关于a的文章,那可能也是我的问题

我对Javascript一无所知,我甚至不知道如何更改按钮的默认行为,如果这是个问题的话


如果有人能在正确的方向上帮助我,使我能够学习和成长,我将不胜感激。

事实上,无论确认提示的结果如何,代码的第二部分(带有警报)都会执行

$.confirm
与VBA的
MsgBox
的主要区别在于
MsgBox
阻止了代码的进一步执行,而
$.confirm
则不会阻止代码的进一步执行。这意味着它后面的代码(if(sResponse==2)部分)立即执行。当时,
sResponse
(因为没有点击任何按钮)没有做任何更改,所以来得太早了

但是,
$.confirm
接受某些函数作为参数,当按下弹出窗口上的按钮时将调用这些函数。实际上,您的代码已经通过了这些函数,但它们除了设置
sResult
之外什么都不做。因此,您应该在这样的函数中移动代码的第二部分(带有警报)

首先,让我们对代码进行一点格式化,使其更具可读性,您可以更好地发现这些回调函数:

if ((sStatus).toUpperCase() != "SUCCESFUL") {
    $.confirm({
        title:  "Confirmation",
        columnClass: 'col-md-6 col-md-offset-3', 
        content:"Forecast calculation still busy. Results might not be accurate. Continue?",
        buttons: {
            confirm: function() {
                // This executes when button is clicked
                sResponse= 1;
            },
            cancel: function() {
                // This executes when button is clicked
                sResponse= 2;
                return;
            }
        }
    });
    // This executes immediately (without waiting for button click)
    if (sResponse == 2  ){
        $.alert({
            title: "INFORMATION",
            columnClass: 'col-md-6 col-md-offset-3',
            content:("Screen will refresh. Please click on Update to try again.").toString(),
        });
        document.getElementById("Smoke.Calc Status").value= "REFRESH";
        msErr = "ABORT";
    }   
}
// Also this executes too early when confirm was executed:
if (vMsg != ""){
    $.alert({
        title: 'Validation Message',
        columnClass: 'col-md-6 col-md-offset-3',    
        content:(vMsg),
    });
    msErr = "ERROR";
}
我添加了注释以显示代码的重要部分在哪里

现在将回调函数中与响应#2相关的第二部分移动:

if(sStatus.toUpperCase()!=“成功”){
美元。确认({
标题:“确认”,
columnClass:'col-md-6 col-md-offset-3',
内容:“预测计算仍在进行中。结果可能不准确。是否继续?”,
按钮:{
确认:函数(){
响应=1;

processMessage();//需要了解的是,
MsgBox()
$.confirm()
并不等同于
MsgBox()
是一个内置的VBScript函数,它生成一个GUI,其中
$.confirm()
只是一个HTML DIV的包装器,其行为类似于弹出窗口,但在完成之前不会被阻止。要做到这一点,您需要将响应代码周围的逻辑移到回调中,而不是只返回
sResponse=2
。这是否回答了您的问题?@Igavshne内置JavaScript
confirm()
函数将更直接地替代VBScript中的
MsgBox()
。这是否回答了您的问题?正如我之前所说,这一切都已经得到了回答。编辑问题以改进它们(例如,澄清、添加附加信息等)鼓励。但是,编辑一个问题以将其更改为另一个问题(这会使一个或多个答案无效)违反堆栈溢出的策略。您在此处的编辑是这样做的。策略是其他具有编辑权限的用户应主动还原此类更改。我已还原了您的编辑。建议您,也许可以通过指向的链接这是一个附加上下文。我们想提供帮助,但您的新问题/附加问题需要是一个新问题。谢谢您,这非常有帮助,特别是因为我有点迷路。是的,我知道您无法测试代码。我会看一看并让您知道。请查看我的更新。恐怕我帮不了您更多。该行为与5 s有关econds必须与您不关心的其他代码相关。我无法重现您的问题。您应该进行调试,使用控制台,设置断点等,以确定发生了什么。谢谢您,我感谢您的时间和帮助。:-)由于似乎还有其他事情需要考虑,我必须先了解整个情况,然后才能决定这个答案是否足够。我每天只为这个雇主工作两个小时,所以可能需要一些时间。不过,我不会忘记你善意而有用的答案。
if (sStatus.toUpperCase() != "SUCCESFUL") {
    $.confirm({
        title:  "Confirmation",
        columnClass: 'col-md-6 col-md-offset-3', 
        content:"Forecast calculation still busy. Results might not be accurate. Continue?",
        buttons: {
            confirm: function() {
                sResponse= 1;
                processMessage(); // <--- added this
            },
            cancel: function() {
                sResponse= 2;
                // Moved code here, as it needs to execute when Cancel is clicked
                $.alert({
                    title: "INFORMATION",
                    columnClass: 'col-md-6 col-md-offset-3',
                    content: "Screen will refresh. Please click on Update to try again.",
                    // Code that should execute when alert is closed:
                    onAction: function () {
                        document.getElementById("Smoke.Calc Status").value= "REFRESH";
                        msErr = "ABORT";
                        processMessage(); // <--- added this
                    }
                });
            },
        }
    });
} else { // <-- added
    processMessage();
}

function processMessage() {
    // Moved code in a function, as it should only execute after confirm/alert is closed 
    if (vMsg != "") {
        $.alert({
            title: 'Validation Message',
            columnClass: 'col-md-6 col-md-offset-3',    
            content: vMsg,
        });
        msErr = "ERROR";
    }
}