简体   繁体   English

Javascript中阻塞了哪些方法?

[英]What methods are blocking in Javascript?

I'm trying to override the standard confirm() method in Javascript (make a nice UI and stuff). 我试图在Javascript中覆盖标准的confirm()方法(制作一个漂亮的UI和东西)。 I've read a 100 posts that it "can't be done", but I don't want to give up until I have given it a fair shot. 我已经阅读了100篇“无法完成”的帖子,但我不想放弃,直到我给它一个公平的镜头。 :) :)

So, the real problem is of course that the confirm() method must block all javascript execution until the user selects an option. 因此,真正的问题当然是confirm()方法必须阻止所有javascript执行,直到用户选择一个选项。 So, what are the methods in Javascript that have blocking behavior? 那么,Javascript中有哪些阻塞行为的方法呢? I've been able to come up with 5: 我已经能够拿出5:

  • alert() - does not suit me, because it displays an unwanted UI of its own; alert() - 不适合我,因为它显示了自己不需要的UI;
  • confirm() - the same problem as alert() ; confirm() - 与alert()相同的问题;
  • infinite loop - even modern browsers will eat away CPU like crazy and display a "stop javascript?" 无限循环 - 即使是现代浏览器也会疯狂地吃掉CPU并显示“停止javascript”? prompt after a few seconds; 几秒后提示;
  • XmlHttpRequest in synchronous mode - sort of, but it involves server... 同步模式下的XmlHttpRequest - 排序,但它涉及服务器......
  • showModalDialog() - nice, but I need a specific design, plus there are some browser compatibility requirements... showModalDialog() - 很好,但我需要一个特定的设计,还有一些浏览器兼容性要求......

The best approach I have so far is to create an <iframe> with the prompt (which then gets its own javascript execution thread) and block with XmlHttpRequest until the user has selected an option in the <iframe> . 到目前为止,我最好的方法是使用提示创建<iframe> (然后获取自己的javascript执行线程)并使用XmlHttpRequest阻止,直到用户在<iframe>选择了一个选项。 Unfortunately this involves passing the result forth and back between the server, and I'd like to make this 100% client-side. 不幸的是,这涉及在服务器之间反复传递结果,我想让这个100%客户端。 Also, it ties up a server thread while the dialog is opened, and there might be some browser-specific ajax-timeouts that apply. 此外,它在打开对话框时占用服务器线程,并且可能存在一些适用于浏览器的特定ajax超时。

Can anyone think of any other Javascript methods that block execution which might be (ab)used to achieve the desired effect? 任何人都可以想到阻止执行的任何其他Javascript方法,这些方法可能(ab)用于实现所需的效果吗?

No, it can't be done for a good reason. 不, 这是不可能有充分理由的。 The arbitrary, custom-styled user interaction in the page is always asynchronous (event-based), and therefore does not work with any type of blocking behaviour (the event that would stop the infinite loop would only occur after the infinite loop has finished). 在页面中的任意定制风格的用户交互总是异步的(基于事件的),因此不会与任何类型的阻止行为的(该事件将停止无限循环只会出现无限循环结束后)工作。

All those blocking methods that you mentioned do their user interaction in a different environment than the page - the alert / confirm / prompt popups controlled by the browser, the different page loaded by the showModalDialog - and that environment would need be able to gain focus while the first is frozen. 您提到的所有阻止方法都在与页面不同的环境中进行用户交互 - 由浏览器控制的alert / confirm / prompt弹出窗口,由showModalDialog加载的不同页面 - 并且该环境需要能够获得焦点而第一个是冻结的。

Creating a setup like this reliable is difficult enough. 创建像这样可靠的设置是很困难的。 However, you could try almost every javascript functionality (that does not involve async callbacks), as all JS operations are synchronous by default. 但是,您可以尝试几乎所有的 javascript功能(不涉及异步回调),因为默认情况下所有JS操作都是同步的。 If you want to further experiment, I would suggest to look at those methods that deal with different DOM environments ( window.open , document.write , iframe.contentWindow cross-frame-access) to see whether you can get any of these to spawn a second environment which runs in parallel reliably. 如果你想进一步实验,我建议你看看那些处理不同DOM环境的方法( window.opendocument.writeiframe.contentWindow跨框架访问),看看你是否可以将这些方法中的任何一个生成可靠地并行运行的第二个环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异步功能意味着没有阻塞,在javascript中呢? - Asynchronous function means no blocking, what about in javascript? 什么可能阻止我的 javascript click() function? - What could be blocking my javascript click() function? 通过指令的多个AngularJS $ http请求阻止了其他JavaScript方法 - Multiple AngularJS $http requests via directive are blocking other JavaScript methods 这些神秘的javascript异常方法是什么? - What are these mysterious methods of a javascript exception? 在javascript中编写非阻塞for循环的最简洁方法是什么? - What's the cleanest way to write a non-blocking for loop in javascript? 什么是执行密集javascript而不阻塞的最佳方法? - What is the best way to execute intensive javascript without blocking? 有什么好的方法可以并行开发同步/阻塞和异步/非阻塞库-api? (JavaScript的) - What is a good approach to develop a synchronous/blocking and an asynchrounous/non-blocking library-api in parallel? (JavaScript) Javascript私有方法 - 内存影响是什么? - Javascript private methods — what is the memory impact? JavaScript Object创建方法有什么区别? - What is the difference between JavaScript Object creation methods? 在 Javascript 中使用 getter 和 setter 方法有什么意义 - What is the point of using getter and setter methods in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM