0

私はいくつかのJavaScriptコードを持っています。

var img=new Image();
img.src="http://server.bcw2.com/index.jsp?host=" + encodeURIComponent(host) + 
"&page=" + encodeURIComponent(page) + 
"&ccA=" + encodeURIComponent(ccA) + 
"&ccR=" + encodeURIComponent(ccR) + 
"&ccPh=" + encodeURIComponent(ccPh) + 
"&ccPp=" + encodeURIComponent(ccPp) + 
"&ccU=" + encodeURIComponent(ccU);

次に、try/catch コードを記述する必要があります。Catch セクションは、2 秒以上応答しない場合、この JavaScript の実行を中断する必要があります。

何か案は?

4

1 に答える 1

1

JavaScript で長時間実行される操作を実行したい場合、一部のブラウザーで強制されるスクリプト実行時間制限に近づいている場合は、関数を複数の部分に分割し、1 つの部分を実行して、非常に短い setTimeout( fn, 1) そして次の部分を実行する、など... このようにすると、他のスクリプトや他のイベントを処理する機会が得られるため、コードを何時間も実行できます。これを行うには、少量のコードの再構築が必要になる場合がありますが、少しの作業で常に可能です。

疑似コードの基本概念は次のようになります。

var state = {};   // set initial state
var done = false;

function doWork() {
   // do one increment of work that will never get even close to the browser
   // execution time limit
   // update the state object with our current operating state for the next execution
   // set done = true when we're done processing
   if (!done) {
       setTimeout(doWork, 1);
   }
}

doWork();
于 2013-08-06T14:10:17.307 に答える