IDBTransaction: abort 事件

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

IndexedDB 事務被中止時,會觸發 abort 事件。

這可能出於以下任何原因:

  • 無效的請求(例如,嘗試兩次新增相同的鍵,或在鍵具有唯一性約束時插入相同的索引鍵)。
  • 顯式呼叫 abort()
  • 請求的 success/error 處理程式中發生未捕獲的異常。
  • I/O 錯誤(例如,磁碟已分離,或其他作業系統/硬體故障,導致實際無法寫入磁碟)。
  • 超出配額。

此不可取消事件會 冒泡 到關聯的 IDBDatabase 物件。

語法

在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。

js
addEventListener("abort", (event) => { })

onabort = (event) => { }

事件型別

一個通用的 Event

事件冒泡

此事件會冒泡到 IDBDatabaseevent.target 屬性指向冒泡上來的 IDBTransaction 物件。

有關更多資訊,請參閱 事件冒泡

示例

此示例開啟一個數據庫(如果資料庫不存在則建立它),然後開啟一個事務,為 abort 事件新增監聽器,然後中止事務以觸發該事件。

js
// Open the database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onupgradeneeded = (event) => {
  const db = event.target.result;

  db.onerror = () => {
    console.log("Error creating database");
  };

  // Create an objectStore for this database
  const objectStore = db.createObjectStore("toDoList", {
    keyPath: "taskTitle",
  });

  // define what data items the objectStore will contain
  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });
};

DBOpenRequest.onsuccess = (event) => {
  const db = DBOpenRequest.result;

  // open a read/write db transaction, ready for adding the data
  const transaction = db.transaction(["toDoList"], "readwrite");

  // add a listener for `abort`
  transaction.addEventListener("abort", () => {
    console.log("Transaction was aborted");
  });

  // abort the transaction
  transaction.abort();
};

與上面相同的示例,但將事件處理程式分配給 onabort 屬性。

js
// Open the database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onupgradeneeded = (event) => {
  const db = event.target.result;

  db.onerror = () => {
    console.log("Error creating database");
  };

  // Create an objectStore for this database
  const objectStore = db.createObjectStore("toDoList", {
    keyPath: "taskTitle",
  });

  // define what data items the objectStore will contain
  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });
};

DBOpenRequest.onsuccess = (event) => {
  const db = DBOpenRequest.result;

  // open a read/write db transaction, ready for adding the data
  const transaction = db.transaction(["toDoList"], "readwrite");

  // add a listener for `abort`
  transaction.onabort = (event) => {
    console.log("Transaction was aborted");
  };

  // abort the transaction
  transaction.abort();
};

規範

規範
Indexed Database API 3.0
# eventdef-idbtransaction-abort

瀏覽器相容性

另見