简体   繁体   English

有没有办法在JSContext对象中停止或暂停执行Javascript?

[英]Is there any way stopping or pausing the execution of Javascript in a JSContext object?

I'm developing an iOS 7 app that has scripting capabilities using JavascriptCore. 我正在开发一个使用JavascriptCore具有脚本功能的iOS 7应用程序。

I'd like to have a way to pause or completely stop the code currently running on the JSContext. 我想有办法暂停或完全停止当前在JSContext上运行的代码。 JavascriptCore isn't really well documented so I couldn't find an answer. JavascriptCore没有很好的文档,所以我找不到答案。

I have a few ideas: 我有一些想法:

  • Remove from the context the bridge object used to interact with my app and just let any code still running fail. 从上下文中删除用于与我的应用程序交互的桥接对象,并让任何代码仍然运行失败。
  • Getting the JSGlobalContextRef for my JSContext and releasing it using JSGlobalContextRelease and recreating a JSContext to use. 获取JSContext的JSGlobalContextRef并使用JSGlobalContextRelease释放它并重新创建要使用的JSContext。

but hopefully there are better ways. 但希望有更好的方法。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

There actually is an API that allows to stop execution of Javascript code inside of JavascriptCore. 实际上有一个API允许停止在JavascriptCore中执行Javascript代码。 Unfortunately though, it is private and thus cannot be used within apps that are to be distributed through the App Store. 不幸的是,它是私有的,因此不能在通过App Store分发的应用程序中使用。

The API in question is the JSContextGroupSetExecutionTimeLimit function defined in JSContextRefPrivate . 有问题的API是JSContextGroupSetExecutionTimeLimit中定义的函数JSContextRefPrivate Not only does it allow to specify a timeout for your JS code, but also a callback that will be called regularly to determine whether the execution should be stopped. 它不仅允许为JS代码指定超时,还允许定期调用以确定是否应该停止执行的回调。

You can find more information in my answer to this SO question. 您可以在我对这个问题的答案中找到更多信息。

If still relevant, you could create a loop that adds the counter (that is going up after every cycle) and the maximum value of the counter. 如果仍然相关,您可以创建一个循环,添加计数器(在每个循环后上升)和计数器的最大值。 I assume you can use a second core since it is called javascript core . 我假设您可以使用第二个核心,因为它被称为javascript 核心 In this second core you can put a function that increases the value of the counter by 10 so it exceeds the max value of the counter and so the code will continue. 在第二个核心中,您可以放置​​一个函数,将计数器的值增加10,使其超过计数器的最大值,因此代码将继续。 You don't need a second core if you can call 2 functions at a time. 如果一次可以调用2个函数,则不需要第二个核心。

Otherwise you can store everything under a different name and overwrite the old ones with nothing. 否则,您可以使用不同的名称存储所有内容,并且无需覆盖旧的名称。

u can use java script timeout callback when on JSContext object is triggered 你可以在触发JSContext对象时使用java脚本超时回调

 function myFunction() { alert('wait is over'); } 
 <!DOCTYPE html> <html> <body> <button onclick="setTimeout(myFunction, 3000);">Click Me</button> <!-- In this context im using a simple button to trigger the call back in your case u can triggeer on jscontext--> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM