Last active
August 29, 2015 14:11
-
-
Save mkropat/e99df893a43100dd2fd3 to your computer and use it in GitHub Desktop.
Sketch of a function to add artificial delays to XHRs
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 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