This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
Note: This feature is available in Web Workers.
The continuePrimaryKey() method of the IDBCursor interface advances the cursor to the item whose key matches the key parameter as well as whose primary key matches the primary key parameter.
A typical use case, is to resume the iteration where a previous cursor has been closed, without having to compare the keys one by one.
Calling this method more than once before new cursor data has been loaded - for example, calling continuePrimaryKey() twice from the same onsuccess handler - results in an InvalidStateError being thrown on the second call because the cursor's got value flag has been unset.
This method is only valid for cursors coming from an index. Using it for cursors coming from an object store will throw an error.
continuePrimaryKey(key, primaryKey)
keyThe key to position the cursor at.
primaryKeyThe primary key to position the cursor at.
None (undefined).
This method may raise a DOMException of one of the following types:
TransactionInactiveError DOMException
Thrown if this IDBCursor's transaction is inactive.
DataError DOMException
Thrown if the key parameter has any of the following conditions:
next or nextunique.prev or prevunique.InvalidStateError DOMException
Thrown if the cursor is currently being iterated or has iterated past its end.
InvalidAccessError DOMException
Thrown if the cursor's direction is not prev or next.
here's how you can resume an iteration of all articles tagged with "javascript" since your last visit:
let request = articleStore.index("tag").openCursor();
let count = 0;
let unreadList = [];
request.onsuccess = (event) => {
let cursor = event.target.result;
if (!cursor) {
return;
}
let lastPrimaryKey = getLastIteratedArticleId();
if (lastPrimaryKey > cursor.primaryKey) {
cursor.continuePrimaryKey("javascript", lastPrimaryKey);
return;
}
// update lastIteratedArticleId
setLastIteratedArticleId(cursor.primaryKey);
// preload 5 articles into the unread list;
unreadList.push(cursor.value);
if (++count < 5) {
cursor.continue();
}
};
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
continuePrimaryKey |
58 | 79 | 10 | 45 | 10.1 | 58 | 51 | 43 | 10.3 | 7.0 | 58 | 10.3 |
IDBDatabase
IDBTransaction
IDBKeyRange
IDBObjectStore
IDBCursor
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/continuePrimaryKey