GAE や Google Closure Library などの話題を扱います。python 初心者です。

2010年2月28日日曜日

Closure Library - XhrManager (2) キャンセル処理

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
(前回)WEB開発メモ: Closure Library - XhrManager

XhrManager 検証続き。

処理をキャンセル(abort)してみる。前回のコードにキャンセルボタンを追加し、これを押した時に XhrIo通信をキャンセルさせる。

function cancel(e) {
  btn_start.setEnabled(true);
  btn_cancel.setEnabled(false);
  for (i=0; i < 20; i++) {
     xhrm_.abort(i);
  }
  alert('cancel');
}

abort() には IDを指定する必要がある。サンプルでは 0〜19 の値を割り当てていたので、ここでは安易に全部の IDで abortをかけてみた。実際には呼び出し側で処理中の ID を覚えておいて必要なものだけ abortするのがいいだろう。

サンプル実行
無事?キャンセルできた。キャンセル時もコールバック関数は呼び出されていることがわかる。またabortをかけられた XhrIo は  getStatus() を呼び出すと -1 を返しているのがわかる。

abortの第2引数は opt_force となっていて、これを true にするとコールバック関数は呼び出されなくなる。



abort(idopt_force)
Aborts the request associated with id.
Arguments:
id :
The id of the request to abort.
opt_force :
boolean=
If true, remove the id now so it can be reused. No events are fired and the callback is not called when forced.
やってみる。
function cancel(e) {
  btn_start.setEnabled(true);
  btn_cancel.setEnabled(false);
  for (i=0; i < 20; i++) {
     xhrm_.abort(i, ture);
  }
  alert('cancel');
}

確かに呼ばれなくなった(-1表示が無い)。

0 件のコメント:

コメントを投稿