【发布时间】:2012-12-26 10:21:12
【问题描述】:
我正在使用以下代码从索引数据库中读取数据并将其保存在变量 allDownloadContent 中
ereaderdownload.indexedDB.getAllTodoItems = function() {
/*var todos = document.getElementById("todoItems");
todos.innerHTML = "";
*/
var db = ereaderdownload.indexedDB.db;
var trans = db.transaction(["downloadcontent"], "readwrite");
var store = trans.objectStore("downloadcontent");
var request = store.get(0);
request.onsuccess = function(e) {
console.log(e.target.result);
};
// Get everything in the store;
var cursorRequest = store.openCursor();
cursorRequest.onsuccess = function(e) {
var result = e.target.result;
if(!!result == false)
return;
allDownloadContent.push(result);
result.continue();
};
alert("content "+allDownloadContent[0]);
cursorRequest.onerror = ereaderdownload.indexedDB.onerror;
};
当我从另一个 Javascript 文件调用 getAllTodoItems 方法时,我收到一条警告消息 content undefined
由于 cursorRequest.onsuccess 方法执行异步,我变得未定义。
我无法使用网络工作者,因为 chrome 不支持它。
我在 Jquery 中尝试过 promise。我仍然收到相同的警报消息。
请帮助我解决问题。
【问题讨论】:
-
Chrome 不支持网络工作者是什么意思? caniuse.com/webworkers
-
我的意思是同步 indexeddb api
-
我猜你想在你编码
return的回调中提醒。那时数据是可用的。
标签: javascript jquery synchronization indexeddb