同步 VBscript 更改为错误的异步 Javascript [英] Synchronous VBscript changed to incorrectly asynchronous Javascript
问题描述
所以我的老板让我调查一个问题,他们将旧的 VBscript 脚本移植到 Javascript 脚本中,但 Javascript 的程序流程不正确.唯一的问题是我真的对这两个都一无所知,但如果你的老板问你,那么你必须制定一个计划.;-)
So my boss asked me to look into an issue where they are porting old VBscript scripts into Javascript scripts but where the Javascript has incorrect program flow. The only problem is that I'm really ignorant about both, but if your boss asks you, then you have to make a plan. ;-)
在 VBscript 中,用户可以选择,如果否"下一个屏幕/视图没有显示.然而,使用 Javascript,无论用户是否选择否/取消",都会加载下一个屏幕/视图.或不.这不应该发生.
In the VBscript, the user was given a choice and if "No" the next screen/view wasn't shown. However, with the Javascript, the next screen/view is loaded irrespective of whether the user chooses "No/Cancel" or not. This should not happen.
这里是 VBscript,重要的部分在//:
Here is the VBscript, the important part is between the //:
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,重要的部分在//:
Here is the Javascript, the important part is between the //:
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 概念,但它似乎是 Internet Explorer 不支持,我们需要记住,我们的一些用户实际上仍在使用 IE ......叹气......因此,我似乎无法使用 async-await 概念,但我不确定在这种情况下有必要这样做.
So, I've seen that there is an async-await-promises concept in Javascript, but it seems that it is not supported by Internet Explorer, and we need to keep in mind that some of our users actually still use IE...sigh... So it seems that I won't be able to use an async-await concept, not that I'm sure that it would be necessary in this situation.
然后,我还在这里阅读了关于表单提交按钮,那也可能是我的问题?
Then, I also read here about a submit button for a form, and that that might be my problem too?
我对 Javascript 一无所知,如果这是问题,我什至不知道如何更改按钮的默认行为.
I'm so clueless about Javascript, I don't even know how to change the button's default behaviour if that is the problem.
如果有人在正确的方向上帮助我,让我能够学习和成长,我将不胜感激.
I would appreciate someone helping me in the right direction so that I can learn and grow.
推荐答案
实际上,无论确认提示的结果如何,代码的第二部分(带有警报)都会执行.
Indeed, the second part of the code (with the alert) is executed irrespective of the outcome of the confirm prompt.
与 $.confirm
和 VBA 的 MsgBox
的主要区别在于 MsgBox
阻止了代码的进一步执行,而 $.confirm
不会这样做.所以这意味着它后面的代码(if (sResponse == 2)
部分)会立即执行.当时没有对 sResponse
进行任何更改(因为没有点击按钮),所以这为时过早.
The main difference with $.confirm
and VBA's MsgBox
, is that MsgBox
is blocking the further execution of code, while $.confirm
does not do that. So that means that the code that follows it (the if (sResponse == 2)
part), is executed immediately. At that time no change was made to sResponse
(as no button was clicked), so that comes too early.
然而,$.confirm
接受一些函数作为参数,当在弹出窗口上按下按钮时会调用这些函数.实际上,您的代码已经通过了这样的函数,但它们除了设置 sResult
之外什么都不做.所以你应该在这样的函数中移动代码的第二部分(带有警报).
However, $.confirm
accepts some functions as arguments, which will be called when a button is pressed on the popup. Actually, your code already passes such functions, but they don't do anything else than setting sResult
. So you should move the second part of the code (with the alert) in such a function.
首先让我们稍微格式化一下您的代码,使其更具可读性,并且您可以更好地发现那些回调函数:
First let's format your code a bit so it becomes more readable, and you can better spot those callback functions:
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";
}
我添加了注释以显示代码的重要部分在哪里.
I added comments to show where the important parts of the code are.
现在将第二部分移动到里面与响应 #2 相关的回调函数:
Now move that second part inside the callback function that relates to response #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";
}
}
我没有测试这段代码,因为它有我不知道的依赖项.
I didn't test this code, as it has dependencies I do not know of.
这篇关于同步 VBscript 更改为错误的异步 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!