做了一个JavaScript程序具有执行多线程? [英] Does a Javascript process have multiple threads of execution?

查看:73
本文介绍了做了一个JavaScript程序具有执行多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个'addressbook'类型的应用程序。有很多加载项。一个想法是最初加载条目的一小部分,以获得用户开始,然后排队的剩余的数据项,该用户点击的条目优先。 (例如,如果他们点击了开始使用X的名字,处理队列中的其余部分之前先加载这些)。我们的想法是加载在初始化时的初始数据集(通过AJAX),然后在后台加载其余(使得很多AJAX来电)。

I am creating an 'addressbook'-type application. There are a lot of entries to load. One idea was to initially load a small subset of the entries, to get the user started, then queue up the remaining entries, giving priority to the entries that the user clicks on. (e.g. if they click on the names that start with X, load those first before processing the rest of the queue). The idea is to load an initial dataset upon initialization (via AJAX) and then load the rest in the background (making a lot of AJAX calls).

在概念上,我知道如何做到这一点,但我不清楚的Javascript引擎的限制:

Conceptually, I know how to do this, but I'm not clear on the limitations of the Javascript engine:

  1. 是为了执行浏览器的依赖?其中一件事我试图做的是排队的条目集(A公司,B公司,C公司等),然后做出一大堆的请求的一次。这并不是很成功。我得到大多数的调用回来,但不以任何特定的顺序。我需要的是我的电话了。 :)

  1. Is order of execution browser dependent? One of the things I tried to do was queue up the set of entries (A's, B's, C's, etc) and then make a whole bunch of requests all at once. This wasn't very successful. I got most of the calls back, but not in any particular order. I need all of my calls back. :)

如何调试这种/跟踪呢?我不知道的Javascript如何处理的响应;这是很容易为一个单一的反应,但我不知道如何处理来自服务器的多个,比较大的反应。

How do I debug this/track this? I'm not sure how Javascript handles the response; it was easy enough for a single response, but I'm not sure how to handle multiple, relatively large responses from the server.

有没有执行的特定网页的单个线程?也就是说,如果Javascript的从服务器获取的响应,但仍执行code,请问事务块,直到当前执行的code结束?

Is there a single thread of execution for a given page? That is, if the Javascript gets a response from the server but is still executing code, does the transaction block until the currently executing code finishes?

会是建议将在请求之间的延迟?这也可能导致问题,因为在装载和发送请求的(截至目前)是在初始化阶段;如果我要请求之间的休眠(),那我还不如干脆强迫用户等待所有数据,但不尝试做到这一点逐步加载加载。

Would it be recommended to put in a delay between requests? This could also cause problems because the loading up and sending of the request (as of now) is in the initialization phase; if I have to sleep() between requests, then I might as well just force the user to wait for the all the data to load without trying to do this gradual loading.

我看了看周围的SO,但我没有找到什么有用的东西。我很好奇如何JS处理这些异步请求/响应。

I looked around on SO but I didn't find anything useful. I'm curious about how JS process these asynchronous requests/responses.

为了给人们更好地了解发生了什么,这里是我在做什么,才能执行。有5个硬codeD搜索类别:名字,姓氏,类别,地区,州。每个类别有范围。例如,第一类的名称可能具有26范围内,一个用于字母表中的每个字母:土豚 - 亚撒利雅将是一个范围的一个例子。每个范围有用户信息在该范围内的每个用户。我有两个表:一个范围表和用户表

Just to give folks a better idea of what's happening, here's what I'm doing in order of execution. There are 5 hard-coded search categories: first names, last names, classes, regions, states. Each of these categories have ranges. For example the first names category might have 26 ranges, one for each letter of the alphabet: "Aardvark - Azariah" would be an example of a range. Each range has the user information for each user in that range. I have two tables: a ranges table and a users table.

  1. 在初始化范围表和范围的数据源对象。地图范围表对象的特定事件。
  2. AJAX调用来获得所有范围为每个类别。我们等待这个,然后再继续。
  3. 在初始化用户表类似范围的表。
  4. 建立一个加载队列让所有用户每个范围。负载队列是这样的:[lastNames ['S'],国['CA'],地区['西北'],lastNames ['A'],等等]
  5. preSELECT一个第一名称类,A的名字范围,并在此范围内的第0个用户。 (这只是一个任意选择,我做给用户一个起点)
  6. AJAX调用来获取所有用户的的firstName ['A']。取下firstNames ['A']的范围从加载队列。
  7. 填充相应的UI元素
  8. 循环通过我们的负载队列,出列范围,构建AJAX请求的数据。

有很多其他的细节太...但是这是它的基本精神。

There are a lot of other details too... but this is the basic gist of it.

会发生什么事是我的表范围内获取填充罚款...但随后的浏览器只有几个街区起来(SPOD),然后我的用户表获取各种疯狂的数据被填充在里面。显然,后者是我的一部分的UI错误,所以我必须以调查,但目前还不清楚,我认为这是要做到这一点的最好办法。

What happens is that my range table gets populated fine... but then the browser just blocks up (spod) and then my users table gets all sorts of crazy data being populated in it. Clearly the latter is a UI bug on my part so I have to investigate that, but it's not clear to me that this is the best way to do this.

在第7步,我不知道是否应该请求之间的延迟。理想情况下,如果用户选择了一个特定的范围,说国家['AK'],我们将首先处理该请求,dq'ing范围从我们的加载队列。但是如果我在前端发送所有的请求,那么我们就没有机会给我们的选择范围适当的优先级。

At step 7, I'm not sure if there should be a delay between requests. Ideally, if a user chose a specific range, say states['AK'], we would process that request first, dq'ing that range from our load queue. But if I'm sending all the requests on the front end, then we'd never get a chance to give our chosen range the appropriate priority.

推荐答案

Javascript是完全单线程的。

Javascript is completely single-threaded.

如果您进行多个Ajax调用,您将会收到每个响应的服务器发送它;顺序取决于所花费服务器发送每个应答的时间量

If you make multiple AJAX calls, you will receive each response as soon as the server sends it; the order depends on the amount of time that it takes the server to send each reply.

如果当服务器回复你的code仍在运行,答复将只在您的code完成后进行处理。

If your code is still running when the server replies, the reply will only be processed after your code finishes.

您应该尝试加载所有的数据在一个单一的请求。

You should try to load all of the data in a single request.

这篇关于做了一个JavaScript程序具有执行多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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