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

浏览器兼容性

另见