模拟 Javascript“警报"阻塞性质 [英] Emulate Javascript 'alert' blocking nature
问题描述
是否可以创建一个自定义模式消息,在用户输入之前阻止"脚本的执行?
Is it possible to create a custom modal message which 'blocks' the execution of the script until a user input?
例如,如何在不使用本机窗口警报/提示功能的情况下实现这一点?
For example, how can you achieve this without using the native window alert / prompt functions?
setInterval(function(){
alert('Click OK to Continue'); // timing stops until user hits ok
},4000);
我知道您可以让您的自定义对话框根据用户输入调用回调函数,但我有兴趣能够强制执行此阻止行为
I know you could have your custom dialog invoke a callback function on user input, but I'm interested in being able to force this blocking behaviour
推荐答案
是否可以创建一个自定义模式消息,在用户输入之前阻止"脚本的执行?
Is it possible to create a custom modal message which 'blocks' the execution of the script until a user input?
没有.没有办法像原生弹出窗口那样有效地阻止执行或用户交互(因为使用自定义弹出窗口,用户在技术上始终能够使用开发者工具摆脱它).
No. There is no way to block either execution or user interaction as effectively as a native popup (since with custom popups the user is always technically capable of using developer tools to get out of it).
然而,正如 pst 在对该问题的评论中所说,异步灯箱并不繁重,并且几乎在阻止用户交互方面与弹出窗口一样有效,因此我建议您找到一个为您提供灯箱的库喜欢并运行它.
However, as pst says in the comments on the question, asynchronous lightboxes are not onerous, and are almost as effective at blocking user interaction as popups, so I recommend finding a library that provides lightboxes you like and running with that.
例如,如何在不使用本机窗口警报/提示功能的情况下实现这一点?
For example, how can you achieve this without using the native window alert / prompt functions?
您无法使用该代码执行您所说的操作,即使本机窗口警报/提示功能(请参阅这个小提琴 - 在关闭弹出窗口前等待 4 秒).您需要以下内容:
You can't use that code to do what you say it will even with native window alert / prompt functions (see this fiddle - wait 4 seconds before closing popup). You'd need the following:
function timeoutFunction() {
alert('Click OK to Continue'); // timing ACTUALLY stops until user hits ok
setTimeout(timeoutFunction, 4000);
}
setTimeout(timeoutFunction,4000);
如果没有本机弹出窗口,您将无法实现(确切地说 - 见上文关于灯箱的内容).
Which is something that you can't implement (precisely - see above on lightboxes) without native popups.
即使 while(true)
循环通常也不会像弹出窗口那样阻塞——firefox 至少有一个停止脚本"消息,在它运行太长时间后弹出,我我相当确定其他主要浏览器也可以.
Even while(true)
loops won't generally block as well as a popup - firefox at least has a "stop script" message that pops up after it's been going too long, and I'm fairly sure other major browsers do too.
这篇关于模拟 Javascript“警报"阻塞性质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!