模拟 Javascript“警报”阻塞性质
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13635297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Emulate Javascript 'alert' blocking nature
提问by lostsource
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
我知道你可以让你的自定义对话框在用户输入上调用回调函数,但我有兴趣能够强制这种阻塞行为
采纳答案by Jeff
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).
不。没有办法像本地弹出窗口那样有效地阻止执行或用户交互(因为使用自定义弹出窗口,用户在技术上始终能够使用开发人员工具摆脱它)。
However, as pst says in the comments on the question, asynchronous lightboxes are not onerous, and are almostas effective at blocking user interaction as popups, so I recommend finding a library that provides lightboxes you like and running with that.
但是,正如 pst 在对该问题的评论中所说的那样,异步灯箱并不繁重,并且在阻止用户交互方面几乎与弹出窗口一样有效,因此我建议找到一个提供您喜欢的灯箱并运行的库。
For example, how can you achieve this without using the native window alert / prompt functions?
例如,如何在不使用本机窗口警报/提示功能的情况下实现这一点?
You can't use that code to do what you say it will even withnative window alert / prompt functions (see this fiddle- wait 4 seconds before closing popup). You'd need the following:
即使使用本机窗口警报/提示功能,您也无法使用该代码执行您所说的操作(请参阅此小提琴- 在关闭弹出窗口之前等待 4 秒)。您需要以下内容:
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.
如果没有本机弹出窗口,您将无法实现(确切地说 - 见上文关于灯箱的内容)。
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.
即使while(true)
循环通常也不会像弹出窗口那样阻塞——firefox 至少有一个“停止脚本”消息,它会在运行时间过长后弹出,我很确定其他主要浏览器也会这样做。
回答by Emil Ivanov
No, you can't (at least not in a browser). Javascript APIs are mostly async. alert
/prompt
are exceptions. However, it's not very hard to work with async prompts and callbacks.
不,你不能(至少不能在浏览器中)。Javascript API 大多是异步的。alert
/prompt
是例外。但是,使用异步提示和回调并不是很难。
回答by Julien Altieri
A bit old, but in case it helps, I've found my solution with this:
有点旧,但如果有帮助,我已经找到了我的解决方案:
var answer = confirm("are you sure?");
if(!answer)return;
var answer = confirm("are you sure?");
if(!answer)return;