VBscript sincrono modificato in JavaScript asincrono in modo errato [duplicato]
Quindi il mio capo mi ha chiesto di esaminare un problema in cui sono stati portati vecchi script VBscript in script Javascript ma dove il Javascript ha un flusso di programma errato. L'unico problema è che sono davvero ignorante su entrambi, ma se il tuo capo te lo chiede, allora devi fare un piano. ;-)
Nel VBscript, all'utente veniva data una scelta e se "No" la schermata / vista successiva non veniva mostrata. Tuttavia, con Javascript, la schermata / vista successiva viene caricata indipendentemente dal fatto che l'utente scelga "No / Annulla" o meno. Questo non dovrebbe accadere.
Ecco il VBscript, la parte importante è tra //:
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
Ecco il Javascript, la parte importante è tra //:
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";
}
Quindi, ho visto che esiste un concetto di asincrono-attesa-promesse in Javascript, ma sembra che non sia supportato da Internet Explorer e dobbiamo tenere presente che alcuni dei nostri utenti in realtà usano ancora IE ... sigh ... Quindi sembra che non sarò in grado di utilizzare un concetto di attesa asincrona, non che sia sicuro che sarebbe necessario in questa situazione.
Quindi, ho anche letto qui su un pulsante di invio per un modulo , e questo potrebbe essere anche il mio problema?
Sono così incapace di Javascript, non so nemmeno come modificare il comportamento predefinito del pulsante se questo è il problema.
Apprezzerei qualcuno che mi aiuti nella giusta direzione in modo da poter imparare e crescere.
Risposte
Infatti, la seconda parte del codice (con l'avviso) viene eseguita indipendentemente dall'esito della richiesta di conferma.
La differenza principale con $.confirme VBA MsgBoxè che MsgBoxblocca l'ulteriore esecuzione del codice, mentre $.confirmnon lo fa. Quindi questo significa che il codice che lo segue (la if (sResponse == 2)parte), viene eseguito immediatamente. In quel momento non è stata apportata alcuna modifica a sResponse(poiché non è stato fatto clic su alcun pulsante), quindi è troppo presto.
Tuttavia, $.confirmaccetta alcune funzioni come argomenti, che verranno chiamati quando si preme un pulsante nel popup. In realtà, il tuo codice passa già tali funzioni, ma non fanno altro che l'impostazione sResult. Quindi dovresti spostare la seconda parte del codice (con l'avviso) in tale funzione.
Per prima cosa formatta un po 'il tuo codice in modo che diventi più leggibile e puoi individuare meglio quelle funzioni di callback:
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";
}
Ho aggiunto commenti per mostrare dove sono le parti importanti del codice.
Ora sposta quella seconda parte all'interno della funzione di callback relativa alla risposta n. 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";
}
}
Non ho testato questo codice, poiché ha dipendenze che non conosco.