【发布时间】:2011-09-25 09:37:56
【问题描述】:
我需要实现事件队列(=服务器上的更新)。当用户更改滑块、按下按钮等时,新事件将添加到此队列中。每个事件将包含以下属性:
- 设备 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 mobile,也许它可以帮助我简化这样的队列创建?
【问题讨论】:
标签: javascript jquery-mobile queue