Хорошо, я думаю, что решил проблему с этим классом «обходного пути».
Синхронизирует звонки и делает их последовательными:
var Syncro = Class.create(
{
initialize: function(params) {
// Check for Prototype class
if(typeof Prototype=='undefined') {
throw("JSSyncro requires the Prototype JavaScript framework to run.");
}
//handle input parameters
this.delay = (typeof params.delay == 'undefined' ? 1000 : params.delay);
this.allowDuplicates = (
typeof params.allowDuplicates=='undefined' || typeof params.allowDuplicates!='boolean'
? true : params.allowDuplicates);
this.order = (
typeof params.order=='undefined' || ['fifo','lifo'].indexOf(params.order)==-1
? 'fifo' : params.order);
this.operations = [];
this.terminated = true;
// private - check for duplicate operations in the stack
this.alreadyExists = function(operation) {
var exists = false;
this.operations.each(
function(element) {
if(element.toString()==operation.toString()) {
exists = true;
return;
}
}
);
return exists;
};
//private - run operations sequentially
this.runSyncronized = function() {
function nextTimeout(instance) {
setTimeout(
function(){
instance.runSyncronized();
},
this.delay);
}
if(this.operations.length>0) {
if(this.terminated) {
this.terminated = false;
var cmd = (this.order=='fifo'
? this.operations.shift() : this.operations.pop());
cmd();
} else {
nextTimeout(this);
}
} else {
this.terminated = true;
}
};
},
// public - wakeup the executor and run the following operation if the previous is terminated
setTerminated: function(boolValue) {
this.terminated = boolValue;
},
// public - set the operation to execute sequentially
execute: function(operation) {
if(this.allowDuplicates || !this.alreadyExists(operation)) {
this.operations.push(operation);
this.runSyncronized();
}
}
}
);
Класс Syncro имеет два основных метода:
execute - где вы можете передать функцию, которую вы должны выполнить внутри анонимной функции.
setTermination - метод установки, полезный для определения завершения асинхронной операции (т. е. установки в методе onComplete асинхронного вызова, выполняемого с помощью ajax).
Функция checkboxClick явно вызывается в событии onclick моих флажков.
Надеюсь, это может быть полезно.
Bye.
Просто эволюция в конструкторе.
Начальные параметры (передаваемые в конструкторе) определяются в массиве:
новый Syncro ({[delay: 1000] [, allowDuplicates: true | false] [, order: 'lifo' | 'fifo']});