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 library
Call jQuery ajaxs requests one by one or parallel
ajaxs
(Object[]) An array of jQuery ajax request settingssettings
(Object: optional) jQuery Ajaxs Settings
AjaxsPromise: The jQuery promise object extended
Like a builder object to call $.ajaxs easily
Ajaxs Settings
type: int
default: 1
How many requests can be processed simultaneously. Set 0 for no limit
Set maxParallel setting
maxParallel
(Int)
Add one more request to call
settings
(Object) jQuery ajax request settings
Aborts the ajaxs
Run all the ajax requests added
AjaxsPromise: The jQuery promise object extended
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
Aborts the ajaxs
<?php
if (isset($_GET['pos'])) {
usleep(rand(300, 1000)*1000);
echo "pos: ".$_GET['pos'];
exit;
}
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);
}
});
}
$.ajaxs(ajaxs, {
maxParallel: 1
})
.done(function() {
console.log('Finished OK');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.error('Finished ERROR ', textStatus, errorThrown);
});
Done! pos: 0
Done! pos: 1
Done! pos: 2
Done! pos: 3
Finished OK
$.ajaxs(ajaxs,
{
maxParallel: 3
})
.done(function() {
console.log('Finished OK');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.error('Finished ERROR ', textStatus, errorThrown);
});
Done! pos: 2
Done! pos: 1
Done! pos: 0
Done! pos: 3
Finished OK
$.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);
});