最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to make timed queue in javascript - Stack Overflow

programmeradmin4浏览0评论

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 bobox. The bobox has extjs data store behind and its getting updated with new data every 5 seconds, i.e the bo 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 bo 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 bo 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 ments 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 bo for the next 5 minutes.

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 bobox. The bobox has extjs data store behind and its getting updated with new data every 5 seconds, i.e the bo 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 bo 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 bo 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 ments 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 bo for the next 5 minutes.

Share Improve this question edited Oct 9, 2012 at 22:51 CWSpear 3,2701 gold badge30 silver badges34 bronze badges asked Oct 9, 2012 at 22:47 VladVlad 2,7737 gold badges52 silver badges106 bronze badges 2
  • 1 If you want an action to happen at a regular interval, you need the "setInterval" mand. – Paul Tomblin Commented Oct 9, 2012 at 22:50
  • @Vlad - did you never find a suitable answer for this? You haven't mented on any of the answers, or the question itself. – FrankieTheKneeMan Commented Apr 13, 2013 at 10:55
Add a ment  | 

5 Answers 5

Reset to default 3

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.

Somewhere else:

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

When adding an element to the bo 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)

Building upon @Gherkin's answer, using jQuery:

function addOption(key, text, timeout) {

    var el = $("<option></option>")
         .attr("value",key)
             .text(text);
    ​$('#test').append(el);
    setTimeout(function() { el.remove(); }, timeout);
}

Now you can call:

addOption("value", "Text for option", 2000);

See it in action: http://jsfiddle/um9Cu/

You could expand addOption() to also lookup an existing item with the key and extend the timeout, or whatever you need to do.

You need to make use of a recursive function that calls setTimeout()

Example:

function myFunc() {
    var minutes = 5;
    //do stuff here
    setTimeout(function() { myFunc() }, minutes*60000 }; 
}

When called myFunc() will fire and then call itself on a 5minute timer. The rest of the programs logic is 100% dependent on end-results.

I solved this problem by usign session. The newest data was written in session, so the new requests were according to the data in session. I used php and js in concert. Js was reading trough php session

发布评论

评论列表(0)

  1. 暂无评论