jquery.ajaxs
v1.2.0
Published
Send jQuery ajax requests one by one or pararell
Downloads
9
Maintainers
Readme
jQuery Ajaxs Plugin
Send jQuery ajax requests one by one or pararell
//two ajaxs requests one by one
$.ajaxs([{
url: '/task/1',
data: 'myparam=testing',
method: 'post'
},{
url: '/task/2',
method: 'post'
}], {
maxParallel: 1
})
.done(function() {
console.log('Both task done one by one!');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log('Something was wrong :( ', errorThrown);
});
Methods
Methods library
$.ajaxs(ajaxs [, settings]): AjaxsPromise
Call jQuery ajaxs requests one by one or parallel
Arguments
ajaxs
(Object[]) An array of jQuery ajax request settingssettings
(Object: optional) jQuery Ajaxs Settings
Returns
AjaxsPromise: The jQuery promise object extended
$.ajaxsManager(): AjaxsManager
Like a builder object to call $.ajaxs easily
Returns
Settings
Ajaxs Settings
maxParallel
type: int
default: 1
How many requests can be processed simultaneously. Set 0 for no limit
AjaxsManager Methods
setMaxParallel(maxParallel): AjaxsManager
Set maxParallel setting
Arguments
maxParallel
(Int)
Returns
add(settings): AjaxsManager
Add one more request to call
Arguments
settings
(Object) jQuery ajax request settings
Returns
abort(): AjaxsManager
Aborts the ajaxs
Returns
run(): AjaxsManager
Run all the ajax requests added
Returns
AjaxsPromise: The jQuery promise object extended
AjaxsPromise Methods
A jQuery Promise object with done, fail, always, pipe... functions.
The deferred is resolved with the last resolve's ajax
The deferred is rejected with the first reject's ajax
abort(): AjaxsPromise
Aborts the ajaxs
Returns
Examples
test.php
<?php
if (isset($_GET['pos'])) {
usleep(rand(300, 1000)*1000);
echo "pos: ".$_GET['pos'];
exit;
}
jQuery Ajax requests
var total = 4;
var ajaxs = [];
for (var i=0; i<total; i++) {
ajaxs.push({
url: 'test.php',
data: {
pos: i
},
success: function(data) {
console.log('Done! '+data);
}
});
}
One by one
$.ajaxs(ajaxs, {
maxParallel: 1
})
.done(function() {
console.log('Finished OK');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.error('Finished ERROR ', textStatus, errorThrown);
});
Ouput:
Done! pos: 0
Done! pos: 1
Done! pos: 2
Done! pos: 3
Finished OK
Parallel
$.ajaxs(ajaxs,
{
maxParallel: 3
})
.done(function() {
console.log('Finished OK');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.error('Finished ERROR ', textStatus, errorThrown);
});
Ouput:
Done! pos: 2
Done! pos: 1
Done! pos: 0
Done! pos: 3
Finished OK
Manager
$.ajaxsManager()
.setMaxParallel(0)
.add({
url: 'test.php',
data: {
pos: 1
}
})
.add({
url: 'test.php',
data: {
pos: 2
}
})
.run()
.done(function() {
console.log('Finish OK!');
})
.fail(function() {
console.log('Finish Error ', arguments);
});