如何在iOS上强制停止JavaScriptCore JSContext评估? [英] How to stop JavaScriptCore JSContext evaluating by force on iOS?

查看:204
本文介绍了如何在iOS上强制停止JavaScriptCore JSContext评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,应该强行停止正在评估的脚本,但我找不到实现此目的的方法.有人指出JSContextGroupSetExecutionTimeLimit可能有用,但是在我的测试中没有,有人可以帮忙吗?

Sometime the script being evaluated should be stopped by force, but I can't find a way to achieve this. Someone pointed out JSContextGroupSetExecutionTimeLimit might work, but It doesn't in my testing, can anyone help?

另一个参考: 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); 
}

这篇关于如何在iOS上强制停止JavaScriptCore JSContext评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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