5

サンプルコード:

var isExecutionOver = false,
    myFunction = function() {
      // does some asynchronous stuff and sets isExecutionOver to true
      // when the asynchronous job is over.
    };

myFunction();

// Kill myFunction if it takes more than 3 seconds
setTimeout(function() {
  if(!isExecutionOver) {
    // How do I kill myFunction?
  }
}, 3*1000);

myFunction上記のスニペットでは、指定された時間 (この場合は 3 秒) 内にジョブを実行できない場合、強制終了 (つまり、実行を停止) しようとしています。

PS: myFunction 定義を制御できないと仮定してください。私が働ける場所は中だけsetTimeoutです。

4

1 に答える 1