gpt4 book ai didi

javascript - 微任务作为javascript代码中的宏任务

转载 作者:行者123 更新时间:2023-12-05 03:19:28 24 4
gpt4 key购买 nike

我找到了 this note在 whatwg 规范中:

It is possible for a microtask to be moved to a regular task queue, if, during its initial execution, it spins the event loop. This is the only case in which the source, document, and script evaluation environment settings object set of the microtask are consulted; they are ignored by the perform a microtask checkpoint algorithm.

表达这句话的例子是什么?如何将微任务从微任务堆栈移动到宏任务堆栈并像宏任务一样执行它?

最佳答案

这里是为了 showModalDialog ,它用于在模式中加载文档,阻塞初始文档,直到关闭此模式...为此,UA 必须 "spin the event loop"直到模态文档关闭。这个“旋转事件循环”宏确实将微任务转换为“简单”任务。

The showModalDialog() method has been removed来自规范,但这仍然存在,因为它也可以用于其他模式或允许“长任务”被 UA 短路,以便他们可以更新渲染,例如向用户显示有关长脚本的消息。
例如,在关于 pausing 的注释中建议这样做, 由所有模态调用,如 alert()prompt() 等:

Pausing is highly detrimental to the user experience, especially in scenarios where a single event loop is shared among multiple documents. User agents are encouraged to experiment with alternatives to pausing, such as spinning the event loop or even simply proceeding without any kind of suspended execution at all, insofar as it is possible to do so while preserving compatibility with existing content.

在某些具有无限循环的浏览器中,它们会暂停脚本并显示一条消息,类似于“此页面上的脚本花费的时间太长,你想等待还是终止用火烧吗?”。

例如,在 Firefox 中,当向用户显示提示时,这两种情况都会触发渲染更新,即它们旋转事件循环。因此,如果“暂停”是从微任务内部触发的,他们应该在恢复时将其移至宏任务。

但是 IIRC,他们实际上使用了自定义版本的旋转事件循环,它只允许渲染阶段(没有回调),仅此而已。和 the microtask isn't really converted to a task even there.
active discussions ,计划在那里“删除这个概念”重新进入微任务,因为它实际上并没有被实现者真正使用。即使是,我也不确定我们是否可以观察到。

关于javascript - 微任务作为javascript代码中的宏任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73436929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com