diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/idbtransaction/complete_event | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/idbtransaction/complete_event')
-rw-r--r-- | files/zh-cn/web/api/idbtransaction/complete_event/index.html | 124 |
1 files changed, 124 insertions, 0 deletions
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 +--- +<div>{{APIRef("IndexedDB")}}</div> + +<p>The <code>complete</code> handler is executed when a transaction successfully completed.</p> + +<table class="properties"> + <tbody> + <tr> + <th scope="row">Bubbles</th> + <td>No</td> + </tr> + <tr> + <th scope="row">Cancelable</th> + <td>No</td> + </tr> + <tr> + <th scope="row">Interface</th> + <td>{{DOMxRef("Event")}}</td> + </tr> + <tr> + <th scope="row">Event handler property</th> + <td>{{DOMxRef("IDBTransaction.oncomplete", "oncomplete")}}</td> + </tr> + </tbody> +</table> + +<h2 id="Examples">Examples</h2> + +<p>Using {{DOMxRef("EventTarget.addEventListener", "addEventListener()")}}:</p> + +<pre class="brush: 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 + 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); +}; + +</pre> + +<p>Using the {{DOMxRef("IDBTransaction.oncomplete", "oncomplete")}} property:</p> + +<pre class="brush: 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 + 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); +};</pre> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.IDBTransaction.complete_event")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/IndexedDB/Using_IndexedDB">Using IndexedDB</a></li> + <li>{{DOMxRef("IDBTransaction.oncomplete", "oncomplete")}} event handler property</li> +</ul> |