IDBOpenDBRequest: blocked Ereignis
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since Juli 2015.
Der blocked-Handler wird ausgeführt, wenn eine offene Verbindung zu einer Datenbank eine versionchange-Transaktion auf derselben Datenbank blockiert.
Dieses Ereignis kann nicht abgebrochen werden und wird nicht weitergeleitet.
Syntax
Verwenden Sie den Ereignisnamen in Methoden wie addEventListener(), oder setzen Sie eine Ereignishandler-Eigenschaft.
js
addEventListener("blocked", (event) => { }) onblocked = (event) => { } Ereignistyp
Ein IDBVersionChangeEvent. Erbt von Event.
Ereigniseigenschaften
Erbt auch Eigenschaften von seiner Elternschnittstelle, Event.
IDBVersionChangeEvent.oldVersionSchreibgeschützt-
Gibt die alte Version der Datenbank zurück.
IDBVersionChangeEvent.newVersionSchreibgeschützt-
Gibt die neue Version der Datenbank zurück.
Beispiele
Verwendung von 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"); }); }; Verwendung der onblocked-Eigenschaft:
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"); }; }; Spezifikationen
| Specification |
|---|
| Indexed Database API 3.0> # eventdef-idbopendbrequest-blocked> |