Instead of moving one line at a time until you hit your random result, how about using the (n) option to jump to a random set? Here is a complete example. It involves two buttons for seeding data and for calling a random selection. I'm going to blog this Monday.
function getRandomInt (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } $(document).ready(function() { var db; var openRequest = indexedDB.open("randomidb",1); openRequest.onupgradeneeded = function(e) { var thisDB = e.target.result; console.log("running onupgradeneeded"); if(!thisDB.objectStoreNames.contains("notes")) { thisDB.createObjectStore("notes", {autoIncrement:true}); } }; openRequest.onsuccess = function(e) { console.log("running onsuccess"); db = e.target.result; $("#seedButton").on("click", function() { var store = db.transaction(["notes"],"readwrite").objectStore("notes"); for(var i=0; i<10; i++) { var note = { title:"Just a random note: "+getRandomInt(1,99999), created:new Date() }; var request = store.add(note); request.onerror = function(e) { console.log("Error",e.target.error.name);
source share