我设计这个功能是为了绕过我的网络应用程序中的复选框。
它在执行时会阻止页面上的所有功能(假设自用户关闭最后一个对话框以来已经过去了不到三秒),但我更喜欢它而不是递归或 setTimeout 函数,因为我不必为这种可能性编写代码等待对话框出现时点击或触发的其他内容。
在 Modalbox 中已包含的报告上显示错误/提示/确认时,我最需要它。我可以为其他对话框添加一个 div,但如果可以使用内置对话框,这似乎太混乱且没有必要。
请注意,如果将 dom.successive_dialog_time_limit 更改为大于 3 的值,这可能会中断,我也不知道 Chrome 是否具有与 Firefox 相同的默认值。但至少这是一种选择。
另外,如果有人可以改进它,请做!
// note that these should not be in the global namespace
var dlgRslt,
lastTimeDialogClosed = 0;
function dialog(msg) {
var defaultValue,
lenIsThree,
type;
while (lastTimeDialogClosed && new Date() - lastTimeDialogClosed < 3001) {
// timer
}
lenIsThree = 3 === arguments.length;
type = lenIsThree ? arguments[2] : (arguments[1] || alert);
defaultValue = lenIsThree && type === prompt ? arguments[1] : '';
// store result of confirm() or prompt()
dlgRslt = type(msg, defaultValue);
lastTimeDialogClosed = new Date();
}
用法:
dialog('This is an alert.');
dialog( 'This is a prompt', prompt );
dialog('You entered ' + dlgRslt);
dialog( 'Is this a prompt?', 'maybe', prompt );
dialog('You entered ' + dlgRslt);
dialog( 'OK/Cancel?', confirm );
if (dlgRslt) {
// code if true
}