IDBOpenDBRequest:blocked 事件
当对数据库的打开连接阻塞了同一数据库上的 versionchange
事务时,将执行 blocked
处理程序。
此事件不可取消,也不冒泡。
语法
在诸如 addEventListener()
的方法中使用事件名称,或设置事件处理程序属性。
js
addEventListener("blocked", (event) => {});
onblocked = (event) => {};
事件类型
一个 IDBVersionChangeEvent
。继承自 Event
。
事件属性
还继承了其父级 Event
接口的属性。
IDBVersionChangeEvent.oldVersion
只读-
返回数据库的旧版本。
IDBVersionChangeEvent.newVersion
只读-
返回数据库的新版本。
示例
使用 addEventListener()
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) => {
// Let's try to open the same database with a higher revision version
const req2 = indexedDB.open("toDoList", 5);
// In this case the onblocked handler will be executed
req2.addEventListener("blocked", () => {
console.log("Request was blocked");
});
};
使用 onblocked
属性
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) => {
// Let's try to open the same database with a higher revision version
const req2 = indexedDB.open("toDoList", 5);
// In this case the onblocked handler will be executed
req2.onblocked = () => {
console.log("Request was blocked");
};
};
浏览器兼容性
BCD 表格仅在启用了 JavaScript 的浏览器中加载。