`title: IndexedDB continuePrimaryKey 主键分页与去抖``categories: Web 开发/前端/数据管理``keywords: IndexedDB,continuePrimaryKey,主键,分页,游标``description: 使用 continuePrimaryKey 基于主键实现稳定分页与滚动加载,并在高频数据流中进行去抖与批处理,提升查询体验。`示例async function pageByPrimaryKey(db, storeName, lastKey, pageSize = 50) { return new Promise((resolve, reject) => { const tx = db.transaction(storeName, 'readonly'); const s = tx.objectStore(storeName); const out = []; s.openCursor().onsuccess = e => { const c = e.target.result; if (!c) return resolve({ items: out, nextKey: undefined }); if (lastKey !== undefined && c.key <= lastKey) { c.continuePrimaryKey(undefined, lastKey); return; } out.push(c.value); if (out.length >= pageSize) return resolve({ items: out, nextKey: c.key }); c.continue(); }; tx.onerror = () => reject(tx.error); }); }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部
2.022351s