JSFiddle - React, Tailwind, and code Playground
by bgmort
JavaScript
Footnote = {};
Footnote.Ajax = function(url, urlParams, method, options) {
var params = {
type: method,
datatype: 'json',
data: urlParams
};
//allow for custom options (such as datatype) to be passed on to jQuery.ajax options
jQuery.extend(params, options);
//not necessary to delete, but not necessary to keep either
delete params.onSuccess;
delete params.onFailure;
delete params.onSlow;
delete params.expectedTime;
delete params.context;
var re = /([^:]+:\/\/[^\/]+)?(.*)/
var currentHost = re.exec(window.location.href)[1]
var match = re.exec(url)
var destinationHost = match[1];
var destinationPath = match[2];
var crossDomain = destinationHost && destinationHost != currentHost;
var timeoutTime = options.expectedTime || 10000;
var timeoutId = null;
var index = -1;
// if they supplied a name, then they want to be able to override concurrent requests
if (options.name) {
if (!Footnote.Ajax.NamedIndexes[options.name]) {
Footnote.Ajax.NamedIndexes[options.name] = 0;
}
// update the counter and remember which one we were
index = ++Footnote.Ajax.NamedIndexes[options.name];
}
// if they supplied a slow callback, be prepared to call it
if (options.onSlow) {
timeoutId = setTimeout(function() {
if (!options.name || Footnote.Ajax.NamedIndexes[options.name] == index) {
options.onSlow.call(options.context);
}
timeoutId = null;
}, timeoutTime);
}
var onFailure = function(type, data, jqXHR) {
// stop any pending timeouts
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
if (!options.name || Footnote.Ajax.NamedIndexes[options.name] == index) {
if (options.onFailure) {
options.onFailure.call(options.context, type, data);
}
...