Created
June 30, 2013 19:30
-
-
Save calvinmetcalf/5896531 to your computer and use it in GitHub Desktop.
better (still bad) ajax worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//your main script | |
function ParalellAjax(){ | |
var ajaxWorker = new Worker('worker.js'); | |
this.close = function(){ | |
ajaxWorker.close(); | |
}; | |
this.data = function(url,callback){ | |
ajaxWorker.onmessage = function(e){ | |
callback(null,e.data); | |
}; | |
ajaxWorker.onerror = function(e){ | |
callback(e); | |
}; | |
ajaxWorker.postMessage(url); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//your worker | |
var quickAjax = function(url,callback){ | |
var ajax = new XMLHttpRequest(); | |
ajax.onreadystatechange = function(resp){ | |
if(ajax.readyState === 4 && ajax.status === 200) { | |
calback(JSON.stringify(ajax.response)); | |
} | |
} | |
ajax.open('GET',url,true); | |
ajax.send(); | |
} | |
self.onmessage = function(e){ | |
quickAjax(e.data,function(resp){ | |
self.postMessage(resp); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment