我开始使用 HTML 5 的索引数据库,但我得到了一些奇怪的结果。第一个是我尝试清除我的数据库,但我只在刷新网站时看到它被重置。事情应该是这样吗? 我见过其他示例代码,但它不会以这种方式发生。
onsuccess 被调用,但更新方法显示的数据库与之前的相同...
这是我的重置功能:
function resetDB()
{
try
{
if (localDatabase != null && localDatabase.db != null)
{
var store = localDatabase.db.transaction("patients", "readwrite").objectStore("patients");
store.clear().onsuccess = function(event)
{
alert("Patients DB cleared");
update_patients_stored();
};
}
}
catch(e)
{
alert(e);
}
}
最佳答案
onsuccess
可以在数据库中实际更新结果之前触发(请参阅 this answer to a question I asked here 。因此,如果 update_ Patients_stored
正在从数据库中读取,它可能仍然会看到旧的数据。如果您使用事务的 oncomplete
,那么您就不会遇到这个问题。
如果这确实导致了您的问题,那么这将解决它:
var tx = localDatabase.db.transaction("patients", "readwrite");
tx.objectStore("patients").clear();
tx.oncomplete = function(event)
{
alert("Patients DB cleared");
update_patients_stored();
};
关于javascript - 我是否需要刷新页面来查看索引数据库是否已重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20078335/