ユーザーがそのアクションの特定の部分を実行したいことを確認するまで、アクション(関数/メソッド呼び出し)を一時停止するための適切なアプローチを探しています。コードの実行を停止できない環境でこれを行う必要があります(私の場合はActionScriptですが、JavaScriptのアプローチは同じである必要があります)。
説明のために、これはユーザープロンプトを導入する前のアクションのモックアップです。
<preliminary-phase> // this contains data needed by all the following phases //
<mandatory-phase> // this will be always be executed //
<optional-phase> // this will always execute too, if in this form, but in some cases we need to ask the user if he wants to do it //
<ending-phase> // also mandatory //
必要なのは、条件付きのユーザープロンプト、「この部分を実行しますか?」を挿入し、<optional-phase>
ユーザーが実行したい場合にのみ実行することです。
<preliminary-phase>
<mandatory-phase>
if(<user-confirmation-is-needed> and not <user-response-is-positive>){
<do-nothing>
}
else{
<optional-phase>
}
<ending-phase>
これをActionScript/JavaScriptで行おうとすると、次のようになります。
<preliminary-phase>
<mandatory-phase>
if(<user-confirmation-is-needed>){
askForConfirmation(callback = function(){
if(<user-response-is-positive>)
<optional-phase>
<ending-phase>
});
return;
}
<optional-phase>
<ending-phase>
<optional-phase>
これで、との両方<ending-phase>
が複製されます。また、で作成されたオブジェクトを使用しているため、<preliminary-phase>
すべてのデータをそれらの関数に渡さずに外部関数に移動することはできません。
私の現在の解決策は、確認を求める前に宣言されたいくつかのローカル関数<optional-phase>
と<ending-phase>
いくつかのローカル関数を囲み<preliminary-phase>
、コードを複製する代わりにそれらの関数を呼び出すことですが、コードは実行された順序ではなくなりました。
何をお勧めしますか?
注:
1。askForConfirmation
は非ブロッキング機能です。これは、その呼び出しに続くコードがすぐに実行されることを意味します(これがreturn;
私のアプローチにある理由です)。