Skip to content

Instantly share code, notes, and snippets.

@EtienneLem
Created June 10, 2013 19:40
Show Gist options
  • Save EtienneLem/5751589 to your computer and use it in GitHub Desktop.
Save EtienneLem/5751589 to your computer and use it in GitHub Desktop.
Ajax Web Worker
var handleRequest = function(data) {
postMessage(data)
}
addEventListener('message', function(e) {
var xhr = new XMLHttpRequest
xhr.open('GET', e.data)
xhr.onreadystatechange = function(e) {
if (xhr.readyState === 4 && xhr.status === 200) {
handleRequest(xhr.responseText)
}
}
xhr.send()
})
// Credit goes to @bdc, see http://sharedfil.es/web-workers-KSaG7cyXFe.html
var handleRequest = function(data) {
postMessage(data)
}
addEventListener('message', function(e) {
importScripts(e.data + "?callback=handleRequest")
})
// Standard Ajax request
var worker = new Worker('ajax_worker.js')
worker.postMessage('/your/ajax_request')
worker.addEventListener('message', function(e) {
data = e.data
})
// JSONP Ajax request
var worker = new Worker('ajax_worker_jsonp.js')
worker.postMessage('http://domain.com/your/ajax_request')
worker.addEventListener('message', function(e) {
data = e.data
})
@freemaster
Copy link

How to use???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment