模仿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?
您不能使用该代码执行您所说的 with 本机窗口警报/提示功能(请参阅这个小提琴 - 在关闭弹出窗口前等待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.
甚至而(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屋!