[英]How to stop JavaScriptCore JSContext evaluating by force on iOS?
有時應該強制停止正在評估的腳本,但我找不到實現此目的的方法。 有人指出JSContextGroupSetExecutionTimeLimit
可能有效,但在我的測試中沒有,有人可以幫忙嗎?
另一個參考: https://github.com/phoboslab/JavaScriptCore-iOS/issues/14
我的代碼:
int extendTerminateCallbackCalled = 0;
static bool extendTerminateCallback(JSContextRef ctx, void *context)
{
extendTerminateCallbackCalled++;
if (extendTerminateCallbackCalled == 2) {
JSContextGroupRef contextGroup = JSContextGetGroup(ctx);
JSContextGroupSetExecutionTimeLimit(contextGroup, .200f, extendTerminateCallback, 0);
return false;
}
return true;
}
+ (void)stop
{
JSGlobalContextRef ref = [_context JSGlobalContextRef];
JSContextGroupRef contextGroup = JSContextGetGroup(ref);
JSContextGroupSetExecutionTimeLimit(contextGroup, .200f, extendTerminateCallback, 0);
}
這是可能的方法的想法
+ (void)stop
{
JSContext *context = [JSContext currentContext]; // or whichever your
// set property of JS global object to specific know value
// that should be checked inside JS code regularly during
// long operations and interrupt self execution
context[@"externallyCancelled"] = @(YES);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.