javascript并行 [英] javascript parallelism

查看:84
本文介绍了javascript并行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,首先我要说的是我在互联网开发领域有点新鲜。

Well, first I want to say I'm a bit new in the world of Internet dev.

无论如何,我试图知道是否可以使用javascript并行运行两段代码。

Anyway, I'm trying to know if its possible to run two pieces of code in parallel using javascript.

我真正需要的是调用远程服务器中的两个方法。我为两者传递了一个回调函数,该函数将在我想要的数据准备就绪后立即执行。由于运行这些功能的服务器需要一些时间来回答,我试图找到一种同时调用这两种方法的方法,而不需要等到第一次完成才能调用第二种方法。

What I really need is to call two methods that are in a remote server. I pass, for both, a callback function that will be executed soon the data I want is ready. As the server running these functions take a time to answer, I'm trying to find a way to call both methods at the same time without need to wait till the first finishes to call the second.

setTimeout等方法是否同时运行,例如

Does methods like setTimeout run concurrently, for example

setTimeout(func1, 0);
setTimeout(func2, 0);

...

function func1()
{
   webMethod1(function() {alert("function 1 returned"); } );
}

function func1()
{
   webMethod2(function() {alert("function 2 returned"); } );
}

已修改

我刚刚发现这篇文章对于下一代浏览器的发布可能非常酷: Javascript网络工作者

I've just found this article that may be very cool for the realease of next browsers: Javascript web workers

推荐答案

执行中有一个单一线程普通WebBrowsers中的Javascript:您的计时器处理程序将被串行调用。您使用计时器的方法适用于您出现的情况。

There is one single thread of execution in Javascript in normal WebBrowsers: your timer handlers will be called serially. Your approach using timers will work in the case you present.

有一个 关于计时器的精彩文档 作者:John Resig(非常受欢迎的jQuery javascript框架的作者 - 如果你是Web开发的新手,我会建议你仔细查看。)

There is a nice piece of documentation on timers by John Resig (author of the very popular jQuery javascript framework - if you are new to Web development, I would suggest you look it up).

现在,如果你指的是基于HTML5的浏览器,那么它们应该有线程支持。

Now, if you are referring to HTML5 based browsers, at some point, they should have threading support.

这篇关于javascript并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆