Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make timed queue in javascript

Ok. I don't have a code for this question. I am asking this because I am new to javascript. I would like to know how to create a timed queue in JS. Here is the trick.

I have a combobox. The combobox has extjs data store behind and its getting updated with new data every 5 seconds, i.e the combo will get new row entries.

The row entries can be of three types 1,2 and 3. There can be several rows of same type but with different row id. Each row will be removed from the combo if there is no update for 5 minutes. This means if I get new row from the store with type 3 it will stay in the combo for 5 minutes. If the same row appears (with the same id and type) in the new data fetched the 5 minutes timer gets reset and again counts 5 minutes. And so forth.

How to achieve this functionality in javascript.

Please write comments if not understood.

Here is an example:

row_1 type1 5 min timer start
row_2 type1 5 min timer start
row_3 type3 5 min timer start
row_2 type2 5 min timer start

This is an example of the current data fetched. After 3 minutes I get this data.

row_3 type3 5 min timer start

the rest of the rows timers continue until 5 min limit is reached but for row three the timer gets reset and it will stay in the combo for the next 5 minutes.

like image 691
Vlad Avatar asked Mar 31 '26 07:03

Vlad


2 Answers

You're going to have to keep track of two things here: The actual item, and when it was last updated (or, more accurately, the timeout event which will update it.

timeouts = [];
elems = [];
function updateElem(no){
    //Do your update on elems[no]
    clearTimeout(timeouts[no]);
    timeouts[no] = setTimeout(function(){ removeElem(no) }, 5*60*1000 );
}
function removeElem(no){
    //Remove the element.
}

This demonstrates the base concept. There are much better ways to keep everything tied together, but the basic idea is:

  • Set a timeout on the object to remove it after five minutes
  • When updating an object:
    • Clear the previous timeout
    • Set a new timeout

Because JS isn't truly multi-threaded, you don't have to worry about concurrency issues, as long as you have your scoping figured out.

Check this documentation to see how the timeout events work in a fuller sense.

like image 126
FrankieTheKneeMan Avatar answered Apr 02 '26 21:04

FrankieTheKneeMan


Somewhere else:

function ComboRemoval(id)
{
    Lookup your combobox (document.findelementbyid perhaps)
    Find element with value "id"
    remove it
}

When adding an element to the combo box:

Add the element
setInterval(function() { ComboRemoval(1234)}, 300000);  //FIXED AS PER COMMENTS BELOW

setInterval will fire function ComboRemoval in 5 minutes, and it will remove ID 1234 from the box.

300000 = 5 minutes (1000 = 1 second, 60 x 5 = 300 seconds, 300 x 1000 = 300000)

like image 21
Gherkin Avatar answered Apr 02 '26 20:04

Gherkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!