Created
August 1, 2016 17:53
-
-
Save jonobr1/7dbbd9d4b7ff168cf54e865a58d211b4 to your computer and use it in GitHub Desktop.
A nano JavaScript object that abstracts xhr requests into jQuery style methods.
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
(function() { | |
var root = this; | |
var previousXhr = root.xhr || {}; | |
var xhr = root.xhr = { | |
noConflict: function() { | |
root.xhr = previousXhr; | |
return xhr; | |
}, | |
get: function(url, callback) { | |
var r = new XMLHttpRequest(); | |
r.open('GET', url); | |
r.onreadystatechange = function() { | |
if (r.readyState === 4 && r.status === 200) { | |
callback(r.responseText); | |
} | |
}; | |
r.send(); | |
return r; | |
}, | |
getBuffer: function(ctx, url, callback) { | |
var r = new XMLHttpRequest(); | |
r.open('GET', url, true); | |
r.responseType = 'arraybuffer'; | |
r.onload = function() { | |
var success = function(buffer) { | |
if (callback) { | |
callback(buffer); | |
} | |
}; | |
var error = function(error) { | |
console.error('decodeAudioData error', error); | |
if (window.ga) { | |
ga('send', 'event', 'video', 'binauralError', url); | |
} | |
}; | |
ctx.decodeAudioData(r.response, success, error); | |
}; | |
r.send(); | |
return r; | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment