我尝试使用以下代码来测试IndexedDB的性能.
代码从http://www.html5rocks.com/en/tutorials/indexeddb/todo/修改,
它在chrome中运行良好,但在Firefox 10中失败,说“db.setVersion不是函数”.
我想知道如何修改代码以使其在Firefox中运行?
var count=0;
var MAX=10;
var times=3;
var allTime;
var stime;
var etime;
var html5rocks = {};
var indexedDB = window.indexedDB || window.webkitIndexedDB ||
window.mozIndexedDB;
if ('webkitIndexedDB' in window) {
window.IDBTransaction = window.webkitIDBTransaction;
window.IDBKeyRange = window.webkitIDBKeyRange;
}
html5rocks.indexedDB = {};
html5rocks.indexedDB.db = null;
html5rocks.indexedDB.onerror = function(e) {
//console.log(e);
alert("Why didn't you allow my web app to use IndexedDB?!");
};
html5rocks.indexedDB.open = function(type) {
var request = indexedDB.open("todos");
request.onsuccess = function(e) {
var v = "1.20";
html5rocks.indexedDB.db = e.target.result;
var db = html5rocks.indexedDB.db;
// We can only create Object stores in a setVersion transaction;
if (v!= db.version) {
var setVrequest = db.setVersion(v);
// onsuccess is the only place we can create Object Stores
setVrequest.onerror = html5rocks.indexedDB.onerror;
setVrequest.onsuccess = function(e) {
if(db.objectStoreNames.contains("todo")) {
db.deleteObjectStore("todo");
}
var store = db.createObjectStore("todo",{keyPath: "number"});
addTest();
};
}
else addTest();
};
request.onerror = html5rocks.indexedDB.onerror;
}
html5rocks.indexedDB.addTodo = function(todoText,num) {
var db = html5rocks.indexedDB.db;
var trans = db.transaction(["todo"],IDBTransaction.READ_WRITE);
var store = trans.objectStore("todo");
var data = {
"text": todoText,"number": num
};
var request = store.put(data);
request.onsuccess = function(e) {
count++;
if(count>=times*MAX)
{
etime=new Date;
var t=document.getElementById('result').innerHTML;
document.getElementById('result').innerHTML=t+((etime.valueOf()-stime.valueOf())/times)+"
";" deltest();="" getting:="" html5rocks.indexeddb.deletetodo="function(id)" datatest();="" html5rocks.indexeddb.adddata="function(d)" data="{" "text":d,"number":1="" function="" addtest()="" for(i="1;i<=times*MAX;i++)" html5rocks.indexeddb.addtodo('="" ',i);="" gettest()="" html5rocks.indexeddb.gettodo(math.round(math.random()*(max*times-1)+1));="" deltest()="" html5rocks.indexeddb.deletetodo(i);="" datatest()="" ;="" stime="new" date="" html5rocks.indexeddb.adddata(data);="" init()="" html5rocks.indexeddb.open();="" <="" code="">
最佳答案