Is there any way stopping or pausing the execution of Javascript in a JSContext object?
Asked Answered
S

3

18

I'm developing an iOS 7 app that has scripting capabilities using JavascriptCore.

I'd like to have a way to pause or completely stop the code currently running on the JSContext. JavascriptCore isn't really well documented so I couldn't find an answer.

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.

but hopefully there are better ways.

Any help would be appreciated. Thanks!

Sojourn answered 13/2, 2014 at 9:58 Comment(2)
Not sure if the is relevant, but using generators should provide a mechanism for starting and stopping the overall execution of the application, but requires you to develop your code in such a way to facilitate that.Jevons
Hi, have you got a good solution.Procure
F
1

There actually is an API that allows to stop execution of Javascript code inside of JavascriptCore. Unfortunately though, it is private and thus cannot be used within apps that are to be distributed through the App Store.

The API in question is the JSContextGroupSetExecutionTimeLimit function defined in 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.

You can find more information in my answer to this SO question.

Fruit answered 18/12, 2018 at 18:33 Comment(1)
This way doesn't work, could you pls take a look at this: github.com/phoboslab/JavaScriptCore-iOS/issues/…Procure
R
0

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 javascriptcore. 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. You don't need a second core if you can call 2 functions at a time.

Otherwise you can store everything under a different name and overwrite the old ones with nothing.

Rosy answered 5/5, 2016 at 13:55 Comment(0)
N
-1

u can use java script timeout callback when on JSContext object is triggered

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>
Nonfulfillment answered 18/9, 2017 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.