javascript 中的多线程和订阅/发布方法
据我所知,javascript 不支持多线程。我想要一些关于以下场景的专家建议。
我的要求是执行 AJAX 调用,成功完成后,我想触发一组事件(并行更新 UI 的不同部分)
- 我计划使用订阅/发布模式,是否可以将多个监听器订阅到 AJAX 完成事件。
如果可能的话,我想知道这些监听器如何在发布时收到通知..(并行地以多线程方式或一一地)。
并建议我实现这一目标的最佳方法..我真的很感激你的想法。
编辑::
我知道有像 JQuery 这样的流行框架支持这种模式。但我处于从头开始开发此功能的情况(我们有自己的框架)。
I understand that there is no multithreading support in javascript. And i wanted some expert advice on the below scenario..
My requirement is to perform a AJAX call and upon successful completetion, i want to trigger set of events (to update the different parts of UI parallely)
- I am planned to use Subscribe/Publish pattern, is it possible to subscribe multiple listners to the AJAX completion event.
If possible, i wanted to know how these listners notified on publish.. (parallely in muthithreaded way or one by one).
And suggest me the best way to achive this one.. I really appreciate your thoughts.
EDIT::
I know there are popular frameworks like JQuery supports this pattern. But am in a situation to develop this functionality from a scratch (we have our own framework).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我有一个请求池程序,它可能会给您一个良好的开端。 [自从这个答案被接受以来,我已经退休了池化器,转而使用更完整的“AJAX Helper” - 链接已更新。]
我不确定这会做你想做的一切(尽管听起来可能是)关闭)。它很旧,但它有效:
Depressed Press DP_AJAX
它支持多个并发请求,并带有超时/重试,每个请求处理程序,占用空间非常小,可以轻松地与其他代码组合。
您创建一个池(告诉它允许有多少个并发请求),然后向它抛出请求。当他们完成后,他们会调用您指定的任何处理程序。
一个完整的小例子:
它是开源的 - 您可以随意切割/折叠/旋转或毁坏它,直到您满意为止。
吉姆·戴维斯
I've a Request Pooler that might give you a good head-start here. [Since this answer was accepted I've retired the pooler in favor of a more complete "AJAX Helper" - the link has been updated.]
I'm not sure that'll do everything you want (although it sounds like it may be close). It's old, but it works:
Depressed Press DP_AJAX
It supports multiple simultaneous requests with timeout/retry, per-request handlers, has a very small footprint and can be combined with other code easily.
You create a pool (telling it how many simultaneous requests are allowed) and then toss requests at it. When they're done they call whatever handler you specified.
A small, complete example of it's use:
It's open source - feel free to chop/fold/spindle or mutilate it to your hearts content.
Jim Davis
本文非常详细地描述了您想要完成的任务。本质上,您只有一个包含处理程序/订阅者数组的 JavaScript 文件。每个订阅者向发布者注册自己(即添加到处理程序数组中)。然后,在 Ajax 调用的 onClose 处理程序中,您将调用一个迭代订阅者并按名称通知每个订阅者的函数:
This article describes what you're trying to accomplish pretty closely. Essentially you just have a JavaScript file that holds an array of handlers/subscribers. Each subscriber registers itself with the publisher (i.e. gets added to the handlers array). Then in the onClose handler of your Ajax call, you'd call a function that iterates over the subscribers and notifies them each by name:
这可以作为轻量级消息传递框架吗?
那么你可以这样做:
然后:
将调度队列
Could this serve as a ligthweight message passing framework?
then you could do:
and later:
would dispatch the queues
这是可能的,您确实应该使用一个库来避免所有浏览器不兼容的情况。例如, jQuery 提供 Ajax 功能,允许您在 Ajax 调用完成时执行代码。请参阅此处获取文档。
This is possible and you should really use a library for that to avoid all the browser incompatibilities. For example jQuery provides Ajax functionality that lets you execute code on completion of the Ajax call. See here for documentation.
如果你想并行触发函数,请使用以下命令:
if you want to fire functions in parralell use following: