javascript – 使用indexedDB时,获取“setVersion”不是函数错误

前端之家收集整理的这篇文章主要介绍了javascript – 使用indexedDB时,获取“setVersion”不是函数错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我尝试使用以下代码来测试IndexedDB的性能.
代码http://www.html5rocks.com/en/tutorials/indexeddb/todo/修改,
它在chrome中运行良好,但在Firefox 10中失败,说“db.setVersion不是函数”.
我想知道如何修改代码以使其在Firefox中运行?

  1. var count=0;
  2. var MAX=10;
  3. var times=3;
  4. var allTime;
  5. var stime;
  6. var etime;
  7. var html5rocks = {};
  8. var indexedDB = window.indexedDB || window.webkitIndexedDB ||
  9. window.mozIndexedDB;
  10. if ('webkitIndexedDB' in window) {
  11. window.IDBTransaction = window.webkitIDBTransaction;
  12. window.IDBKeyRange = window.webkitIDBKeyRange;
  13. }
  14. html5rocks.indexedDB = {};
  15. html5rocks.indexedDB.db = null;
  16. html5rocks.indexedDB.onerror = function(e) {
  17. //console.log(e);
  18. alert("Why didn't you allow my web app to use IndexedDB?!");
  19. };
  20. html5rocks.indexedDB.open = function(type) {
  21. var request = indexedDB.open("todos");
  22. request.onsuccess = function(e) {
  23. var v = "1.20";
  24. html5rocks.indexedDB.db = e.target.result;
  25. var db = html5rocks.indexedDB.db;
  26. // We can only create Object stores in a setVersion transaction;
  27. if (v!= db.version) {
  28. var setVrequest = db.setVersion(v);
  29. // onsuccess is the only place we can create Object Stores
  30. setVrequest.onerror = html5rocks.indexedDB.onerror;
  31. setVrequest.onsuccess = function(e) {
  32. if(db.objectStoreNames.contains("todo")) {
  33. db.deleteObjectStore("todo");
  34. }
  35. var store = db.createObjectStore("todo",{keyPath: "number"});
  36. addTest();
  37. };
  38. }
  39. else addTest();
  40. };
  41. request.onerror = html5rocks.indexedDB.onerror;
  42. }
  43. html5rocks.indexedDB.addTodo = function(todoText,num) {
  44. var db = html5rocks.indexedDB.db;
  45. var trans = db.transaction(["todo"],IDBTransaction.READ_WRITE);
  46. var store = trans.objectStore("todo");
  47. var data = {
  48. "text": todoText,"number": num
  49. };
  50. var request = store.put(data);
  51. request.onsuccess = function(e) {
  52. count++;
  53. if(count>=times*MAX)
  54. {
  55. etime=new Date;
  56. var t=document.getElementById('result').innerHTML;
  57. document.getElementById('result').innerHTML=t+((etime.valueOf()-stime.valueOf())/times)+"
最佳答案
该规范尚未最终确定.目前,这是作为Gecko中的属性mozIndexedDB和Chrome中的webkitIndexedDB提供的,直到标准最终确定.所以你必须为moz写作.现在这段代码仅适用于webkit.

https://developer.mozilla.org/en/IndexedDB

猜你在找的JavaScript相关文章