大网数据广告活动
Web 标准与前端开发

IDBCursor: continuePrimaryKey() method

· 52CE 我爱测

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.

IndexedDB

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.

Syntax

continuePrimaryKey(key, primaryKey)

Parameters

  • key
    • : The key to position the cursor at.
  • primaryKey
    • : The primary key to position the cursor at.

Return value

None (undefined).

Exceptions

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:
      • 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 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.

Examples

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();
  }
};

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。