IDBDatabase:close() 方法
注意:此功能在 Web Workers 中可用。
IDBDatabase 接口的 close() 方法会立即返回,并在一个单独的线程中关闭连接。
在所有使用此连接创建的事务完成之前,连接实际上并未关闭。调用此方法后,不能再为该连接创建新事务。如果存在待处理的关闭操作,则创建事务的方法会抛出异常。
语法
js
close()
参数
无。
返回值
无(undefined)。
示例
js
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4); // opening a database.
// Create event handlers for both success and failure of
DBOpenRequest.onerror = (event) => {
  note.appendChild(document.createElement("li")).textContent =
    "Error loading database.";
};
DBOpenRequest.onsuccess = (event) => {
  note.appendChild(document.createElement("li")).textContent =
    "Database initialized.";
  // store the result of opening the database in the db variable.
  db = DBOpenRequest.result;
  // now let's close the database again!
  db.close();
};
规范
| 规范 | 
|---|
| Indexed Database API 3.0 # ref-for-dom-idbdatabase-close② | 
浏览器兼容性
加载中…
另见
- 使用 IndexedDB
- 开始事务:IDBDatabase
- 使用事务:IDBTransaction
- 设置键的范围:IDBKeyRange
- 检索和修改数据:IDBObjectStore
- 使用游标:IDBCursor
- 参考示例:待办事项通知(查看实时示例)。