JavaScript中的事件优先级是什么? [英] What is the event precedence in JavaScript?

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

问题描述



以下是按字母顺序排列的事件...



on
  • onabort - 加载图片是
    中断

  • onblur - 元素失去焦点

  • onchange - 用户更改字段的
    内容

  • onclick - 鼠标单击对象

  • ondblclick - 鼠标双击
    对象

  • onerror -
    加载文档或图像时发生错误

  • onfocus - 元素获取焦点

  • onkeydown - 键盘键

  • onkeypress - 一个键盘键是
    按下或按住

  • onkeyup - 键盘键
    发布

  • onload - 页面或图像是
    完成加载

  • onmousedown - 一个鼠标按钮是
    按下

  • onmousemove - 鼠标移动

  • onmouseout - 鼠标移开
    元素

  • onm ouseover - 鼠标移动
    超过元素

  • onmouseup - 鼠标按钮是
    发布

  • onreset - 重置按钮是
    点击

  • onresize - 窗口或框架是
    调整大小

  • onselect - 选择文本

  • onsubmit - 提交按钮是
    点击

  • onunload - 用户退出页面

  • 他们从事件队列中处理了什么顺序?



    优先级不是先进先出(FIFO)或所以我相信。

    解决方案

    据我所知,这不是过去明确定义的。不同的浏览器可以自由地实现事件排序,但他们认为合适。虽然大多数情况对于所有实际目的都足够接近,但是已经并且仍然是一些奇怪的情况,其中浏览器有所不同(当然,更多的情况是某些浏览器无法发送某些事件 em>)。



    那就是说, HTML 5草案建议确实试图指定事件如何排队和调度 - 事件循环


    要协调事件,用户
    的交互,脚本,渲染,
    网络等等,用户代理
    必须使用
    本节所述的事件循环。 p>

    每个用户代理必须至少有一个事件循环
    ,最多一个事件
    循环每单位相关的
    相似原点眉头唱上下文。



    事件循环有一个或多个任务
    队列。一个任务队列是一个订单
    任务列表
    当用户代理队列一个任务时,
    它必须将给定的任务添加到
    之一相关事件
    循环的任务队列。来自一个
    特定任务源的所有任务必须始终为
    添加到同一个任务队列中,但来自不同任务源的
    任务可能会将
    置于不同的任务队列中。
    [...]



    [...]用户代理可能有一个任务队列
    用于鼠标和键事件(用户
    交互任务源),另一个
    。用户代理
    可以给键盘和鼠标
    事件首选项超过其他任务
    四分之三的时间,保持
    接口响应但不
    饿死其他任务队列,从来没有
    处理事件从任何一个任务
    源无序。 [...]


    注意最后一点:由浏览器实现决定哪些事件将被组合在一起并处理以及对任何特定类型的事件的优先权。因此,现在或将来,没有理由期望所有浏览器都以固定的顺序发送所有的事件。


    What order of precedence are events handled in JavaScript?

    Here are the events in alphabetical order...

    1. onabort - Loading of an image is interrupted
    2. onblur - An element loses focus
    3. onchange - The user changes the content of a field
    4. onclick - Mouse clicks an object
    5. ondblclick - Mouse double-clicks an object
    6. onerror - An error occurs when loading a document or an image
    7. onfocus - An element gets focus
    8. onkeydown - A keyboard key is pressed
    9. onkeypress - A keyboard key is pressed or held down
    10. onkeyup - A keyboard key is released
    11. onload - A page or an image is finished loading
    12. onmousedown - A mouse button is pressed
    13. onmousemove - The mouse is moved
    14. onmouseout - The mouse is moved off an element
    15. onmouseover - The mouse is moved over an element
    16. onmouseup - A mouse button is released
    17. onreset - The reset button is clicked
    18. onresize - A window or frame is resized
    19. onselect - Text is selected
    20. onsubmit - The submit button is clicked
    21. onunload - The user exits the page

    What order are they handled out of the event queue?

    The precedence is not first-in-first-out (FIFO) or so I believe.

    解决方案

    This was not, so far as i know, explicitly defined in the past. Different browsers are free to implement event ordering however they see fit. While most are close enough for all practical purposes, there have been and continue to be some odd edge cases where browsers differ somewhat (and, of course, the many more cases where certain browsers fail to send certain events at all).

    That said, the HTML 5 draft recommendation does make an attempt to specify how events will be queued and dispatched - the event loop:

    To coordinate events, user interaction, scripts, rendering, networking, and so forth, user agents must use event loops as described in this section.

    There must be at least one event loop per user agent, and at most one event loop per unit of related similar-origin browsing contexts.

    An event loop has one or more task queues. A task queue is an ordered list of tasks [...] When a user agent is to queue a task, it must add the given task to one of the task queues of the relevant event loop. All the tasks from one particular task source must always be added to the same task queue, but tasks from different task sources may be placed in different task queues. [...]

    [...]a user agent could have one task queue for mouse and key events (the user interaction task source), and another for everything else. The user agent could then give keyboard and mouse events preference over other tasks three quarters of the time, keeping the interface responsive but not starving other task queues, and never processing events from any one task source out of order. [...]

    Note that last bit: it is up to the browser implementation to determine which events will be grouped together and processed in order, as well as the priority given to any particular type of event. Therefore, there's little reason to expect all browsers to dispatch all events in a fixed order, now or in the future.

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

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