什么是浏览器事件循环? [英] What is a browser event loop?

查看:162
本文介绍了什么是浏览器事件循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用GWT进行一些Web应用程序编程,并被浏览器事件循环一词所困惑。



我遇到了需要执行的情况延迟命令和浏览器事件循环完成后执行某些操作。



我想知道到底是什么,在事件循环过程中会发生什么哪个订单?

解决方案

浏览器事件循环是由浏览器启动的线程,它不断扫描并运行不同的事件,喜欢听起来当事件发生时,它们被放在事件队列中,并依次由一个事件线程运行。您的javascript不应该创建自己的循环等待它完成或任何类似的...它将阻止一个连续的事件循环线程。相反,您将使用类似setTimeout或setInterval的类型,并检查您正在等待的任何条件,以便浏览器在等待时可以进行工作。



GWT在它可以使用调度程序 - 在您希望在事件循环完成之后运行某些事件的情况下,您可能需要使用scheduleFinally或scheduleDeferred。它将为事件队列中的一段代码注入一个处理程序,以便它将在当前执行上下文中的所有其他代码(当前执行上下文==)之后运行,您当前在当前JavaScript对象层次结构中以窗口为根对象)运行,但在放置在队列中的下一个事件之前。


I have been doing some web application programming using GWT and have been confused by the term "browser event loop".

I have encountered situations where I need to execute deferred commands and "do something" after the browser event loop completes.

I would like to know as to what exactly it is and what happens during the event loop process and in which order?

解决方案

A browser event loop is a thread started by the browser that is constantly scanning for and running different events, just like it sounds. As events occur they are put in the event queue and run in turn by the one event thread. Your javascript should not create its own loops waiting for it to complete or anything like that...it will block that one continuous event loop thread. Instead you would use something like setTimeout or setInterval and check for whatever conditions you are waiting for so the browser can do work while it 'waits'.

GWT is nice in that it can co-opt this process somewhat using the scheduler -- in your case where you want to run something after the event loop 'completes' you will probably want to use scheduleFinally or scheduleDeferred. It will inject a handler for a piece of code into the event queue so that it will run after all other code in the current execution context (current execution context == where ever you are in the current JavaScript object hierarchy with the window as the root object) is run but before the next event that is placed in the queue.

这篇关于什么是浏览器事件循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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