IDBTransaction: commit()-Methode
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.
Hinweis: Diese Funktion ist inWeb Workers verfügbar.
Diecommit()
-Methode desIDBTransaction
-Interfaces bestätigt die Transaktion, wenn sie auf einer aktiven Transaktion aufgerufen wird.
Beachten Sie, dasscommit()
normalerweise nichtaufgerufen werden muss – eine Transaktion wird automatisch bestätigt, wenn alle ausstehenden Anfragen erfüllt sind und keine neuen Anfragen gestellt wurden.commit()
kann verwendet werden, um den Bestätigungsprozess zu starten, ohne auf Ereignisse von ausstehenden Anfragen zu warten.
Wenn es auf einer Transaktion aufgerufen wird, die nicht aktiv ist, wird einInvalidStateError
DOMException
ausgelöst.
Syntax
commit()
Parameter
Keine.
Rückgabewert
Keiner (undefined
).
Ausnahmen
InvalidStateError
DOMException
Wird ausgelöst, wenn der Transaktionszustand nicht aktiv ist.
Beispiele
const note = document.getElementById("notifications");// open a read/write db transaction, ready for adding the dataconst transaction = db.transaction(["myDB"], "readwrite");// report on the success of opening the transactiontransaction.oncomplete = (event) => { note.appendChild(document.createElement("li")).textContent = "Transaction completed: database modification finished.";};transaction.onerror = (event) => { note.appendChild(document.createElement("li")).textContent = "Transaction not opened due to error. Duplicate items not allowed.";};// create an object store on the transactionconst objectStore = transaction.objectStore("myObjStore");// add our newItem object to the object storeconst objectStoreRequest = objectStore.add(newItem[0]);objectStoreRequest.onsuccess = (event) => { // report the success of the request (this does not mean the item // has been stored successfully in the DB - for that you need transaction.onsuccess) note.appendChild(document.createElement("li")).textContent = "Request successful.";};// Force the changes to be committed to the database asaptransaction.commit();
Spezifikationen
Specification |
---|
Indexed Database API 3.0 # ref-for-dom-idbtransaction-commit② |
Browser-Kompatibilität
Siehe auch
- Verwendung von IndexedDB
- Starten von Transaktionen:
IDBDatabase
- Verwendung von Transaktionen:
IDBTransaction
- Festlegen eines Schlüsselbereichs:
IDBKeyRange
- Abrufen und Ändern Ihrer Daten:
IDBObjectStore
- Verwendung von Cursorn:
IDBCursor
- Referenzbeispiel:To-do Notifications (Beispiel live ansehen).
MDN-Feedback-Box
Diese Seite wurde automatisch aus dem Englischen übersetzt.