Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IndexedDB with Web Workers
<html>
<body>
<span id="output"></span>
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="main.js"></script>
</html>
var worker = new Worker('worker.js');
worker.onmessage = function(event) {
$('#output').text('Output is: ' + event.data);
};
worker.postMessage('foo');
self.onmessage = function(event) {
var req = indexedDB.open('mydb', 1);
req.onupgradeneeded = function (e) {
self.postMessage('successfully upgraded db');
};
req.onsuccess = function (e) {
self.postMessage('successfully opened db');
};
req.onerror = function(e) {
self.postMessage('error');
}
};
@questsin

This comment has been minimized.

Copy link

@questsin questsin commented Mar 28, 2016

Nice example. Does "req" only work inside of self.onmessage? I tried putting it all inside a function inside worker.js that self.onmessage calls but it fails to call "onsucess"? any ideas?

  • var req = indexedDB.open...
  • req..onsuccess = function (e) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment