continuePrimaryKey() 方法在 IDBCursor interface advances the cursor to the 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.

注意: 此特征可用于 Web 工作者 .

句法

cursor.continuePrimaryKey(key, primaryKey);
					

参数

key

The key to position the cursor at.

primaryKey

The primary key to position the cursor at.

异常

此方法可能引发 DOMException of one of the following types:

异常 描述
TransactionInactiveError This IDBCursor's transaction is inactive.
DataError The key parameter may have any of the following conditions:

  • The key is not a valid key.
  • The key is less than or equal to this cursor's position and the cursor's direction is next or nextunique .
  • The key is greater than or equal to this cursor's position and this cursor's direction is prev or prevunique .
InvalidStateError The cursor is currently being iterated or has iterated past its end.
InvalidAccessError 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();
    }
};
				

规范

规范 状态 注释
索引数据库 API 草案
The definition of 'continuePrimaryKey()' in that specification.
推荐

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
continuePrimaryKey Chrome 58 Edge 79 Firefox 51
51
不支持 10 — 51 Prefixed
Prefixed Implemented with the vendor prefix: moz
IE ? Opera 45 Safari 10.1 WebView Android 58 Chrome Android 58 Firefox Android 51 Opera Android 43 Safari iOS 10.3 Samsung Internet Android 7.0

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: