Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkropat/e99df893a43100dd2fd3 to your computer and use it in GitHub Desktop.
Save mkropat/e99df893a43100dd2fd3 to your computer and use it in GitHub Desktop.
Sketch of a function to add artificial delays to XHRs
function wrapInDelay(obj, methodName, delay) {
initialize();
wrapMethod(obj, methodName, function (method, args) {
window._delayerWantsDelay = delay;
var result = method.apply(this, args);
delete window._delayerWantsDelay;
return result;
});
function initialize() {
if (window._delayerInited)
return;
window._delayerInited = true;
wrapMethod(jQuery, 'ajax', ajaxWrapper);
}
function ajaxWrapper(ajaxMethod, args) {
if (window._delayerWantsDelay) {
var delay = +window._delayerWantsDelay || 0;
setTimeout(function () {
ajaxMethod.apply(this, args);
}, delay);
}
else {
return ajaxMethod.apply(this, args);
}
}
function wrapMethod(obj, methodName, wrapper) {
var original = obj[methodName];
obj[methodName] = function () {
var args = Array.prototype.slice.call(arguments, 0);
return wrapper.call(obj, original, args);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment