1

I have JS thats called using stringByEvaluatingJavaScriptFromString and takes around 9 seconds to run, but I'd like to be able to cancel this immediately if the user so requires.

It seems however that the entire thread is blocked by this.

I would be prepared to do anything to allow the user to cancel this (e.g. remove the UIWebView ) but nothing seems to work as the app waits for the stringByEvaluatingJavaScriptFromString to return before continuing.

1 Answer 1

0

Not sure how to cancel it. In order to improve the UI, from this UIWebView stringByEvaluatingJavaScriptFromString in background,

Try splitting up your JavaScript into discrete execution blocks and pipeline them using a JavaScript timer, like this (JS code, not Obj-C):

var i = 0;
var operation = function() {

    switch (i) {
    case 0:
       //do first part of code
       break;
    case 1:
       //do second part of code
       break;
    case 2:
       //do third part of code
       break;
    etc...
    }

    //prepare to execute next block
    i++;
    if (i < TOTAL_PARTS) {
        setTimeout(operation, 0);
    }
};
operation();

That will prevent your script from blocking user interaction while it executes.

For more details please check that thread, UIWebView stringByEvaluatingJavaScriptFromString in background.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.