0

I'm trying to push objects on to array and process the array as a queue. Nothing is currently removing anything from the array, and each time the acme.addToValidateQueue function is called ( several times in succession ) it always shows 1 as the array length. What am I doing wrong?

var acme = {};
acme.validateQueue = [];
acme.validateQueueLastIndex = 0;

acme.addToValidateQueue = function(fieldData,fieldName){
    var queueItem = {
        index : acme.validateQueueLastIndex,
        fieldData : fieldData,
        fieldName : fieldName
    };
    acme.validateQueue.push(queueItem);
    acme.validateQueueLastIndex++;
    console.log(acme.validateQueue.length); // shows 1 everytime the queue is called.
    if(acme.validateQueue.length === 1){
        acme.processValidateQueue();
    };
}

acme.processValidateQueue = function(){
    if(acme.validateQueue.length){
        acme.validate_field(acme.validateQueue.shift());
    }
}
cactusphone
  • 507
  • 4
  • 15

1 Answers1

2

Every time you call addToValidateQueue, that calls processValidateQueue, which promptly removes the item you just added:

acme.validate_field(acme.validateQueue.shift());
//                            Right here ^

That means that on the next call, you only see the item you're adding on that call. The old item is gone.

user2357112
  • 260,549
  • 28
  • 431
  • 505