Last active
August 29, 2015 14:16
-
-
Save kishmiryan-karlen/8fc6d1223810f349bb4f to your computer and use it in GitHub Desktop.
Simple simulator for jQuery's ajax() method
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
// 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