PDA

View Full Version : Synchronous number of queue



newbieCoder
02-07-2012, 04:53 PM
Any elites here? I want to seek help.... again :(:(

I want to count the number in queue while some ppl come and leave.

I can count the number when no ppl leave. But when the ppl starts to leave,
the number in queue becomes inaccurate.

Here is my program running without compiling error.

<html>
<head>
<title>
JavaScript array Queue
</title>
<script type="text/javascript">
var globalCounterTable1=0;
var arr = new Array();
var instantNumInTable = new Array();
var customer, queueCounter=0, totalInTable=0;


function ArrayQueuePush(arrayName,arrayElement){
arrayName.push(arrayElement);

}

function ArrayQueuePop(arrayName){
arrayName.shift();
globalCounterTable1=globalCounterTable1-1;
}

function enqueue(){
var input1 = window.prompt("Enter the value you want to enqueue: ");
customer = parseFloat(input1)
instantNumInTable[globalCounterTable1]=customer;
globalCounterTable1=globalCounterTable1+1;

totalInTable=totalInTable+customer;

ArrayQueuePush(arr,input1);
document.panel.showQueue.value = arr;
document.panel.table1.value = totalInTable;
setTimeout('dequeue()', 10000);
}

function dequeue(){
ArrayQueuePop(arr);
document.panel.showQueue.value = arr;
totalInTable=0;
for (var i=globalCounterTable1; i>0; i--){
totalInTable=totalInTable+instantNumInTable[i];}
document.panel.table1.value = totalInTable;

}

</script>
</head>
<body bgcolor="#ddcdff">
<h2>
JavaScript array Queue Example
<form name="panel">
<INPUT TYPE = "button" name="enQueue" value = "Enqueue" onclick = "enqueue()">
<INPUT TYPE = "button" name="deQueue" value = "Dequeue" onclick = "dequeue()">
<br />Array:<INPUT TYPE = "text" id = "showQueue" name="showQueue" size="20">
<br />Number of Customers in Table 1: <INPUT TYPE = "text" id = "table1" name="table1" size="20">
</form>
</h2>
</body>
</html>