简体   繁体   English

取消stringByEvaluatingJavaScriptFromString

[英]Cancel stringByEvaluatingJavaScriptFromString

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. 我有JS thats使用stringByEvaluatingJavaScriptFromString调用,并且需要大约9秒钟来运行,但是如果用户需要,我希望能够立即取消它。

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 (eg remove the UIWebView ) but nothing seems to work as the app waits for the stringByEvaluatingJavaScriptFromString to return before continuing. 我将准备做任何事情来允许用户取消此操作(例如,删除UIWebView),但似乎无济于事,因为该应用程序等待stringByEvaluatingJavaScriptFromString返回然后再继续。

Not sure how to cancel it. 不知道如何取消它。 In order to improve the UI, from this UIWebView stringByEvaluatingJavaScriptFromString in background , 为了改善用户界面, 请在后台从此UIWebView stringByEvaluatingJavaScriptFromString开始

Try splitting up your JavaScript into discrete execution blocks and pipeline them using a JavaScript timer, like this (JS code, not Obj-C): 尝试将JavaScript拆分为离散的执行块,然后使用JavaScript计时器进行流水线处理,如下所示(JS代码,而不是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 . 有关更多详细信息,请检查线程UIWebView stringByEvaluatingJavaScriptFromString在background中

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

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