如何等待回调,直到用户按下bootboxJS提示上的任何按钮,而不是在显示提示后执行剩余的脚本



此函数位于js函数的一个非常大的层次结构中,因此不能使用视图中的html,否则我将丢失所有参数,因为每个脚本中都传递了许多参数。

使用bootbox.js进行提示——因为它在提示中屏蔽了不起作用的代码的密码。我需要等待回调,而不是在显示提示后完成剩余脚本的执行。

function authorize() {
alert("Script has started...");
var final_result = 0;
bootbox.prompt({
title: "This is a prompt with a password input!",
inputType: 'password',
callback: function(result)
{
if (result!=null) 
{
final_result = 1;
} else {
final_result = 0;
}
}
});
alert("calling XYZ");  
function xyz();
alert("Script has finished... final result is" + final_result);  
}

使用默认JavaScript提示的工作代码——想要这样(使用bootboxjs只是屏蔽输入,就像我使用它作为密码一样(:

function authorize() {
alert("Script has started...");
var final_result = 0;
let text = prompt("This is a prompt with a password input!", "")
if (text!= null) {
final_result = 1;
} else {
final_result = 0;
}
alert("calling xyz" );  
function xyz();
alert("Script has finished... final result is" + final_result );  
}

文档中多次介绍了这一点:

与本机警报、确认或提示不同,所有Bootstrap模式(以及Bootbox模式(都是异步的;也就是说,Bootstrap生成的事件是非阻塞事件。由于此限制,必须在对话框的回调函数中调用在用户解除对话框之前不应该评估的代码。

举个例子,我假设你想要的是:

function authorize() {
alert("Script has started...");

let final_result = 0;
bootbox.prompt({
title: "This is a prompt with a password input!",
inputType: 'password',
callback: function(result) {
if (result != null) {
final_result = 1;
} 
else {
final_result = 0;
}

// call xyz() here, with your prompt's value
xyz(final_result);
}
});
}
// Changed this to accept a parameter, to match what 
// it seems you're trying to do
function xyz(final_result) {
alert("Script has finished... final result is" + final_result);  
}

重申一下:引导模式只是定位<div>元素,而modal()函数只是隐藏和显示它们

如果您真的想要一个可自定义的提示来阻止程序执行,那么您唯一的选择就是<dialog>(请参阅MDN(。不过,它最近才获得足够的浏览器支持,所以使用风险自负。

最新更新