IDBRequest:readyState 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

注意:此功能在 Web Workers 中可用。

IDBRequest 接口的只读属性 readyState 返回请求的状态。

每个请求都始于 pending 状态。当请求成功完成或发生错误时,状态会变为 done

以下字符串之一

pending

如果请求仍在进行中,则返回此值。

done

如果请求已完成,则返回此值。

示例

以下示例请求一个给定的记录标题,onsuccessIDBObjectStore 中获取相关记录(可通过 objectStoreTitleRequest.result 访问),更新记录的一个属性,然后将更新后的记录放入另一个请求中的对象存储中。第二个请求的 readyState 将被记录到开发者控制台中。有关完整的可运行示例,请参阅我们的 To-do Notifications 应用(在线查看示例)。

js
const title = "Walk dog";

// Open up a transaction as usual
const objectStore = db
  .transaction(["toDoList"], "readwrite")
  .objectStore("toDoList");

// Get the to-do list object that has this title as its title
const objectStoreTitleRequest = objectStore.get(title);

objectStoreTitleRequest.onsuccess = () => {
  // Grab the data object returned as the result
  const data = objectStoreTitleRequest.result;

  // Update the notified value in the object to "yes"
  data.notified = "yes";

  // Create another request that inserts the item
  // back into the database
  const updateTitleRequest = objectStore.put(data);

  // Log the readyState of this request
  console.log(
    `The readyState of this request is ${updateTitleRequest.readyState}`,
  );

  // When this new request succeeds, run the displayData()
  // function again to update the display
  updateTitleRequest.onsuccess = () => {
    displayData();
  };
};

规范

规范
Indexed Database API 3.0
# ref-for-dom-idbrequest-readystate①

浏览器兼容性

另见