Skip to content

Instantly share code, notes, and snippets.

@JokingChicken
Last active September 11, 2018 10:02
Show Gist options
  • Save JokingChicken/25eacc24f7d8fe5a3c09ca506a6f3dba to your computer and use it in GitHub Desktop.
Save JokingChicken/25eacc24f7d8fe5a3c09ca506a6f3dba to your computer and use it in GitHub Desktop.
When uploading something get the procentage
<script>
/*usage:
request("get", "https://github.com/DanBrothers/danbrothers.github.io/blob/master/js/main.js", null,
function(status, data) {
if(status==="percent") {
console.log("file progress: " + data + "%");
}
if(...) {
}
}
);
*/
function request(method, url, data, cb) {
var ajax = new XMLHttpRequest();
ajax.addEventListener("progress", function go(event) {cb("percent", ((event.loaded / event.total) * 100))}, false);
ajax.addEventListener("load", function go(event) {cb("eventloaded")}, false);
ajax.addEventListener("error", function go(event) {cb("error")}, false);
ajax.addEventListener("abort", function go(event) {cb("aborted")}, false);
ajax.open(method, url, true);
ajax.send(data);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment