同步 VBscript 更改为不正确的异步 Javascript
Synchronous VBscript changed to incorrectly asynchronous Javascript
所以我的老板让我调查一个问题,他们正在将旧的 VBscript 脚本移植到 Javascript 脚本中,但是 Javascript 的程序流程不正确。
唯一的问题是,我对两者都一无所知,但如果你的老板问你,那你就得制定计划。 ;-)
在 VBscript 中,用户可以选择,如果选择“否”,则不会显示下一个 screen/view。
但是,对于 Javascript,无论用户是否选择“No/Cancel”,都会加载下一个 screen/view。这不应该发生。
这里是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
这里是Javascript,重要的部分在//:
之间
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中有一个async-await-promises的概念,但好像是not supported by Internet Explorer,我们需要记住一些我们的用户实际上还在使用 IE...唉...
所以看起来我将无法使用异步等待概念,但我不确定在这种情况下是否有必要。
然后,我在这里也看到了一个 submit button for a form,这可能也是我的问题?
我对 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() != "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";
}
}
我没有测试这段代码,因为它有我不知道的依赖项。
所以我的老板让我调查一个问题,他们正在将旧的 VBscript 脚本移植到 Javascript 脚本中,但是 Javascript 的程序流程不正确。 唯一的问题是,我对两者都一无所知,但如果你的老板问你,那你就得制定计划。 ;-)
在 VBscript 中,用户可以选择,如果选择“否”,则不会显示下一个 screen/view。 但是,对于 Javascript,无论用户是否选择“No/Cancel”,都会加载下一个 screen/view。这不应该发生。
这里是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
这里是Javascript,重要的部分在//:
之间 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中有一个async-await-promises的概念,但好像是not supported by Internet Explorer,我们需要记住一些我们的用户实际上还在使用 IE...唉... 所以看起来我将无法使用异步等待概念,但我不确定在这种情况下是否有必要。
然后,我在这里也看到了一个 submit button for a form,这可能也是我的问题?
我对 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() != "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";
}
}
我没有测试这段代码,因为它有我不知道的依赖项。