1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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>
|