From 74471b7051b3fd0e7a9ee7c75c7229542f760131 Mon Sep 17 00:00:00 2001 From: fruedadina <46003506+fruedadina@users.noreply.github.com> Date: Wed, 19 Dec 2018 17:47:53 +0100 Subject: [PATCH] Adding sync support. If you launch vajax from a third plane with "async: false" option, don't work because "while(xhr.readyState != 4) { xhr.processEvents(); }" don't work for sync procedure. Now can skip this code if sync procedure is apply. xhr.open() has fixed with async option, because "options.async || true" is true always. Now is true by default or false if you want sync procedure. --- ajax.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ajax.js b/ajax.js index 679934c..93a62f2 100644 --- a/ajax.js +++ b/ajax.js @@ -150,11 +150,12 @@ function getHeaders(headersString) { $ = {ajax: function(options) { var data = options.data || {}, - headers = options.headers || {}, - url = "", + headers = options.headers || {}, + url = "", body = null, xhr = new XMLHttpRequest(), - i; + i, + async = options.async == undefined ? true : options.async; function jsonToParams(json) { var params = [], i; @@ -227,9 +228,11 @@ $ = {ajax: function(options) { } }; - xhr.open(options.type.toUpperCase(), url, options.async || true); + xhr.open(options.type.toUpperCase(), url, async); for (i in headers) { xhr.setRequestHeader(i, headers[i]); } if ( body ) { xhr.send(body); } else { xhr.send(); } - while(xhr.readyState != 4) { xhr.processEvents(); } + if (async) { + while(xhr.readyState != 4) { xhr.processEvents(); } + } } -}; \ No newline at end of file +};