JS_SetOperationCallback 编辑

Obsolete since JSAPI 30
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

This article covers features introduced in SpiderMonkey 1.8.5

Set a callback function that is automatically called periodically while JavaScript code runs. These methods/types are renamed to JS_SetInterruptCallback, JS_GetInterruptCallback, JS_RequestInterruptCallback and JSInterruptCallback in SpiderMonkey 30.

Syntax

void
JS_SetOperationCallback(JSContext *cx, JSOperationCallback callback);

JSOperationCallback
JS_GetOperationCallback(JSContext *cx);

void
JS_TriggerOperationCallback(JSRuntime *rt);
NameTypeDescription
cxJSContext *A context.
rtJSRuntime *The runtime.
callbackJSOperationCallback(only in JS_SetOperationCallback) The callback function to install.

Callback syntax

JSBool
(*JSOperationCallback)(JSContext *cx);
NameTypeDescription
cxJSContext *

Pointer to a JSContext in which this callback was installed.  The callback may use this context to call JSAPI functions, but it should first use JS_SetOperationCallback to set the context's operation callback to NULL. Otherwise the engine may call the operation callback again, reentering it.

Provides request. In JS_THREADSAFE builds, the JavaScript engine calls this callback only from within an active request on cx. The callback does not need to call JS_BeginRequest()).

Applications must not use both the operation callback API and the older branch callback API.

Description

These functions allow setting an operation callback that will be called from the JS thread some time after any thread triggered the callback using JS_TriggerOperationCallback.
To schedule the GC and for other activities the engine internally triggers operation 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_SetOperationCallback sets a callback that can be called asynchronously. Some common uses for an operation 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 JS_FALSE.)

JS_GetOperationCallback returns the currently installed operation callback, or NULL if none is currently installed.

JS_TriggerOperationCallback triggers a callback set using JS_SetOperationCallback.

JS_ClearOperationCallback clears the current operation callback. Obsolete since JavaScript 1.9.1

JS_SetOperationCallbackFunction sets the operation callback without changing the operation limit. Obsolete since JavaScript 1.9.1

JS_GetOperationLimit returns the current operation limit, or JS_MAX_OPERATION_LIMIT if no operation callback is currently installed. Obsolete since JavaScript 1.9.1

JS_SetOperationLimit sets the operation limit without changing the operation callback. It must be called only when an operation callback is installed. Obsolete since JavaScript 1.9.1

See Also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:138 次

字数:7377

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文