我需要实现事件队列(=服务器上的更新)。新的事件将被添加到用户时改变所述滑块这个队列,按下按钮等。每个事件将包含以下属性:如何实现事件队列?
- 装置ID(动作将被应用到该设备的服务器上)
- 操作(设置,获取等)
- 值(value,应该在行动中使用)
新的事件应该有在年底增加。但是,如果已经有相同设备ID和相同操作的事件,则应该使用新值更新此事件。我应该怎么做?
我已经起草了以下内容:
var inCall = false;
var queueArrayDevices = new Array();
var queueArrayActions = new Array();
var queueArrayValues = new Array();
// add call to the queue, at the end
function addAPICall(device, action, value){
// should NOT add event here, if device and action already exists
// should update the value instead
queueArrayDevices.push(device);
queueArrayAсtions.push(action);
queueArrayValues.push(value);
}
function doAPICall(device, action, value){
inCall = true;
// call server here
// if not successful, we should add this item to the queue again
inCall = false;
}
function callAPIQueue(){
if(!inCall && queueArrayDevices.length > 0){
device = queueArrayDevices.shift();
action = queueArrayAсtions.shift();
value = queueArrayValues.shift();
doAPICall(device, action, value);
}
}
// start queue processing
setInterval(callAPIQueue, 400);
使用jQuery移动,可能是它可以帮助我来简化这些队列创建?
谢谢,马丁。这几乎是我正在寻找的。只需要一个改变 - 事件应该从'doAPICall'的队列中删除,而不是提前(因为我需要等待它的回复,所以我在那里调用服务器)。 –
那么您可以将事件从callAPIQueue队列中移动到doAPICall,并且应该按照您的意愿进行操作。 –