JS_SetInterruptCallback 编辑
This article covers features introduced in SpiderMonkey 31
Set a callback function that is automatically called periodically while JavaScript code runs.
Syntax
JSInterruptCallback
JS_SetInterruptCallback(JSRuntime *rt, JSInterruptCallback callback);
JSInterruptCallback
JS_GetInterruptCallback(JSRuntime *rt);
void
JS_RequestInterruptCallback(JSRuntime *rt);
Name | Type | Description |
---|---|---|
rt | * | The runtime. |
callback | JSInterruptCallback | The callback function to install. |
Callback syntax
bool
(* JSInterruptCallback)(JSContext *cx);
Name | Type | Description |
---|---|---|
cx |
| Pointer to a Provides request. In |
Description
These functions allow setting an interrupt callback that will be called from the JS thread some time after any thread triggered the callback using JS_RequestInterruptCallback
.
To schedule the GC and for other activities the engine internally triggers interrupt callbacks. The embedding should thus not rely on callbacks being triggered through the external API only.
Important note: Additional callbacks can occur inside the callback handler if it re-enters the JS engine. The embedding must ensure that the callback is disconnected before attempting such re-entry.
JS_SetInterruptCallback
sets a callback that can be called asynchronously. Some common uses for an interrupt callback are:
- To run garbage collection periodically, by calling
;JS_MaybeGC
- To periodically take a break from script execution to update the UI (though note that Mozilla does not do this, by design);
- To enforce application limits on the amount of time a script may run. (In this case, the callback may terminate the script by returning
false
.)
JS_GetInterruptCallback
returns the currently installed interrupt callback, or NULL
if none is currently installed.
JS_RequestInterruptCallback
requests a callback set using JS_SetInterruptCallback
.
See Also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论