From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../api/idbtransaction/complete_event/index.html | 124 +++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 files/zh-cn/web/api/idbtransaction/complete_event/index.html (limited to 'files/zh-cn/web/api/idbtransaction/complete_event/index.html') diff --git a/files/zh-cn/web/api/idbtransaction/complete_event/index.html b/files/zh-cn/web/api/idbtransaction/complete_event/index.html new file mode 100644 index 0000000000..d3767e086e --- /dev/null +++ b/files/zh-cn/web/api/idbtransaction/complete_event/index.html @@ -0,0 +1,124 @@ +--- +title: 'IDBTransaction: complete event' +slug: Web/API/IDBTransaction/complete_event +translation_of: Web/API/IDBTransaction/complete_event +--- +
{{APIRef("IndexedDB")}}
+ +

The complete handler is executed when a transaction successfully completed.

+ + + + + + + + + + + + + + + + + + + + +
BubblesNo
CancelableNo
Interface{{DOMxRef("Event")}}
Event handler property{{DOMxRef("IDBTransaction.oncomplete", "oncomplete")}}
+ +

Examples

+ +

Using {{DOMxRef("EventTarget.addEventListener", "addEventListener()")}}:

+ +
// 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
+  var 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 `complete`
+  transaction.addEventListener('complete', event => {
+    console.log('Transaction was competed');
+  });
+
+  const objectStore = transaction.objectStore('toDoList');
+  const newItem = { taskTitle: 'my task', hours: 10, minutes: 10, day: 10, month: 'January', year: 2019 };
+  const objectStoreRequest = objectStore.add(newItem);
+};
+
+
+ +

Using the {{DOMxRef("IDBTransaction.oncomplete", "oncomplete")}} property:

+ +
// 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
+  var 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 `complete`
+  transaction.oncomplete = event => {
+    console.log('Transaction was competed');
+  };
+
+  const objectStore = transaction.objectStore('toDoList');
+  const newItem = { taskTitle: 'my task', hours: 10, minutes: 10, day: 10, month: 'January', year: 2019 };
+  const objectStoreRequest = objectStore.add(newItem);
+};
+ +

Browser compatibility

+ + + +

{{Compat("api.IDBTransaction.complete_event")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf