检查 JavaScript setTimeout 是否已触发
我希望能够通过 JavaScript 分派大量工作在浏览器中完成,从而使浏览器始终保持响应能力。
我尝试采用的方法是将工作分块,将每个块传递给一个函数,然后使用 setTimeout(func, 0)
调用对该函数进行排队。
我需要知道所有工作何时完成,因此我将返回的计时器 ID 存储在映射中(id -> true|false)。在获得计时器 ID 后,此映射在下一个代码块中设置为 false,并且排队函数在完成时将映射设置为 true...当然,除非排队函数不知道其计时器 ID。
也许有更好/更简单的方法...或者一些关于如何根据需要操纵地图的建议?
I'd like to be able to dispatch a bunch of work via JavaScript to be done in the browser in such a way that the browser stays responsive throughout.
The approach I'm trying to take is to chunk up the work, passing each chunk to a function that is then queued with a setTimeout(func, 0)
call.
I need to know when all the work is done, so I'm storing the returned timer ID in a map (id -> true|false). This mapping is set to false in the next block of code after I have the timer ID, and the queued function sets the mapping to true when it completes... except, of course, the queued function doesn't know its timer ID.
Maybe there's a better/easier way... or some advice on how I can manipulate my map as I need to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将工作放入一个数组中进行排队,使用一个超时来处理队列,并在队列为空时调用回调。比如:
由于浏览器中的 JavaScript 是单线程的,因此运行多个超时没有真正的优势(至少我认为这就是你正在做的事情)。它甚至可能会减慢浏览器的速度。
I would queue the work in an array, use one timeout to process the queue and call a callback once the queue is empty. Something like:
As JavaScript in browsers is single threaded there is no real advantage to run multiple timeouts (at least I think this is what you are doing). It may even slow down the browser.
我想补充一点,虽然 javascript 是单线程的,但您仍然可以同时进行多个 ajax 调用。我最近有一个网站需要执行数百个 ajax 调用,而浏览器无法处理它。我创建了一个使用 setTimeOut 一次运行 5 个调用的队列。当其中一个 ajax 调用返回时,它会触发一个回调(由单个线程处理),然后在堆栈上进行下一个调用。
想象一下,您是一名经理,一次只能与一个人交谈,您给 5 名员工分配了任务,然后等待他们的答复,答复的顺序可能不限。一旦第一位员工回来并向您提供信息,您就给他们分配新的任务并等待下一位员工(甚至可能是同一位员工)回来。因此,尽管你是“单线程”,但有 5 件事会同时发生。
I'd like to add that although javascript is single threaded you can still have multiple ajax calls going at once. I recently had a site that needed to do potentially hundreds of ajax calls and the browser just couldn't handle it. I created a queue that used setTimeOut to run 5 calls at once. When one of the ajax calls returned it fired a callback (which is handled by a single thread) and then made the next call on the stack.
Imagine you're a manager that can only talk to one person at a time, you give 5 employees assignments, then wait for their responses, which may come in any order. Once the first employee comes back and gives you the information, you give them a new assignment and wait for the next employee (or perhaps even the same employee) to come back. So although you're "single threaded" 5 things are going on at once.
HTML 标准中有一个示例,最好如何处理:
当调用
clearTimeout
时,完成工作的时刻非常清楚。There is an example right in the HTML Standard, how it is best to handle it:
The moment of finishing the work is pretty clear, when
clearTimeout
is called.