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①

瀏覽器相容性

另見