简体   繁体   English

如何在 javascript 中计算 y 时间内的 x 事件数?

[英]How to calculate x number of events over y amount of time in javascript?

I have a class that receives an event, let's call it "EVENT_X".我有一个接收事件的 class,我们称之为“EVENT_X”。

It can receive this event many times over any amount of duration, but I need to know if it crosses a threshold lets say 10 "EVENT_X" events in 5 minutes, so I can take an action accordingly.它可以在任何时间长度内多次接收此事件,但我需要知道它是否超过阈值,比如 5 分钟内发生 10 个“EVENT_X”事件,因此我可以采取相应的措施。

I am unable to figure out appropriate algorithm for this and also a one which will be efficient, I've just tried waiting till first 10 events come and checking if time is < 5, of course that doesn't solve the purpose.我无法为此找出合适的算法,也无法找到一种有效的算法,我只是尝试等到前 10 个事件到来并检查时间是否 < 5,当然这并不能解决目的。

Any help is much appreciated, thankyou!非常感谢任何帮助,谢谢!

Use a global array as stack for the date-objects when the button last have been clicked.上次单击按钮时,使用全局数组作为日期对象的堆栈。 Everytime it's clicked just another one is appended.每次单击它时,都会附加另一个。 If there at least 5 cut it on the last 5 and look for the difference from now to the first click in milliseconds.如果在最后 5 次中至少有 5 次,则在毫秒内查找从现在到第一次点击的差异。 If it's smaller than 5000 than do what you want eg change the color of the button by adding a class or make output to the console.如果它小于 5000 而不是您想要的,例如通过添加 class 或使 output 到控制台来更改按钮的颜色。

Note: If you are to slow (more than 5s) than you have to click some more faster till the last 5 clicks in time.注意:如果您要放慢速度(超过 5 秒),则您必须更快地点击,直到最后 5 次点击。 For demonstration I take only 5 clicks and 5 s, but you can easily change it to your desire.为了演示,我只需要 5 次点击和 5 秒,但您可以轻松地将其更改为您的愿望。

 var counter = []; document.getElementById('btn').addEventListener('click', function() { let now = new Date(); counter.push(now); if (counter.length >=5) { counter = counter.slice(-5); let diff = now - counter[0]; if (diff <= 5000) { document.getElementById('btn').classList.add('x5'); console.log('Clicked 5x in the time.'); } } });
 .x5 { background: yellow; }
 <button id='btn'>Click it 5x</button>

So add an object and log when the event took place.所以添加一个 object 并记录事件发生的时间。 When you add the event, filter out any events greater than 5 minutes.添加事件时,过滤掉任何超过 5 分钟的事件。 Check length.检查长度。 If hits the max, fire off whatever you were going to do next.如果达到最大值,则启动您接下来要执行的任何操作。

 const evtTypes = {}; function logEvent (event) { const { type } = event; evtTypes[type] = evtTypes[type] || []; evtTypes[type].push({ ts: new Date().getTime(), data: event }); evtTypes[type] = evtTypes[type].filter(({ ts }) => new Date().getTime() - ts < 300000); if (evtTypes[type].length >= 10) { console.log(`"${event.type}" triggered 10 times in 5 minutes.`); } } logEvent({ type: 'test', foo: 'bar01' }); logEvent({ type: 'test', foo: 'bar02' }); logEvent({ type: 'test', foo: 'bar03' }); logEvent({ type: 'test', foo: 'bar04' }); logEvent({ type: 'test', foo: 'bar05' }); logEvent({ type: 'test', foo: 'bar06' }); logEvent({ type: 'test', foo: 'bar07' }); logEvent({ type: 'test', foo: 'bar08' }); logEvent({ type: 'test', foo: 'bar09' }); logEvent({ type: 'test', foo: 'bar10' });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM