Skip to content

Instantly share code, notes, and snippets.

@kishmiryan-karlen
Last active August 29, 2015 14:16
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 kishmiryan-karlen/8fc6d1223810f349bb4f to your computer and use it in GitHub Desktop.
Save kishmiryan-karlen/8fc6d1223810f349bb4f to your computer and use it in GitHub Desktop.
Simple simulator for jQuery's ajax() method
// Notes:
// 1. Use the second parameter to simulate a response data.
// 2. Use the third (boolean) parameter to simulate a failed request.
// jQuery's AJAX simulator
var $ = {
ajax: function(opts, returnData, throwError) {
console.group('AJAX Request');
console.log('Request URL: ', opts.url);
console.log('Request method: ', opts.method);
console.log('Request data: ', opts.data);
console.groupEnd('AJAX Request');
var timer = Math.round() * 10 + 1;
if (throwError && typeof opts.error === 'function') {
setTimeout(function() {
opts.error(new Error('AJAX request error'));
}, timer);
return;
}
if (typeof opts.success === 'function') {
setTimeout(function() {
opts.success(returnData);
}, timer);
}
}
};
// Usage example
$.ajax({
url: 'http://google.com',
method: 'GET',
success: function(data) {
console.log(data);
},
error: function(err) {
console.log(err);
}
}, 'test data', true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment