jquery queue ajax,Queue ajax requests using jQuery.queue()

just another example of a multi threaded queue runner i wrote for nodejs. You could adapt it to jquery or angular. Promises are slightly different in each API. I've used this pattern for things like extracting all items from large lists in SharePoint by creating multiple queries to fetch all data and allowing 6 at a time, to avoid server-imposed throttling limits.

/*

Job Queue Runner (works with nodejs promises): Add functions that return a promise, set the number of allowed simultaneous threads, and then run

(*) May need adaptation if used with jquery or angular promises

Usage:

var sourcesQueue = new QueueRunner('SourcesQueue');

sourcesQueue.maxThreads = 1;

childSources.forEach(function(source) {

sourcesQueue.addJob(function() {

// Job function - perform work on source

});

}

sourcesQueue.run().then(function(){

// Queue complete...

});

*/

var QueueRunner = (function () {

function QueueRunner(id) {

this.maxThreads = 1; // Number of allowed simultaneous threads

this.jobQueue = [];

this.threadCount = 0;

this.jobQueueConsumer = null;

this.jobsStarted = 0;

if(typeof(id) !== 'undefined') {

this.id = id;

}

else {

this.id = 'QueueRunner';

}

}

QueueRunner.prototype.run = function () {

var instance = this;

return new Promise(function(resolve, reject) {

instance.jobQueueConsumer = setInterval(function() {

if(instance.threadCount < instance.maxThreads && instance.jobQueue.length > 0) {

instance.threadCount++;

instance.jobsStarted++;

// Remove the next job from the queue (index zero) and run it

var job = instance.jobQueue.splice(0, 1)[0];

logger.info(instance.id + ': Start job ' + instance.jobsStarted + ' of ' + (instance.jobQueue.length + instance.jobsStarted));

job().then(function(){

instance.threadCount--;

}, function(){

instance.threadCount--;

});

}

if(instance.threadCount < 1 && instance.jobQueue.length < 1) {

clearInterval(instance.jobQueueConsumer);

logger.info(instance.id + ': All jobs done.');

resolve();

}

}, 20);

});

};

QueueRunner.prototype.addJob = function (func) {

this.jobQueue.push(func);

};

return QueueRunner;

}());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值