我有一個用於與IndexedDB進行交互的對象。它有一個屬性,當數據庫完成打開時,它會更改爲2。我如何告訴read方法等待this.ready == 2?使方法暫停,直到另一個屬性準備好
var db = {
open: function() {//a callback sets this.ready to 2 when the database has been successfully opened},
ready: 0,
read: function() {
if(!this.ready == 2) {//pause until this.ready == 2}
...
}
}
我應該寫一個函數中使用的setTimeout來檢查this.ready每隔100毫秒或者是有一個更優雅的解決方案?
這可能有助於http://stackoverflow.com/questions/5346694/how-to-implement-a-lock-in-javascript – Tija