简体   繁体   English

如何解决JavaScript中的实时交易问题?

[英]How to solve a real time transaction problem in JavaScript?

I encounter two problems regarding the transaction which needs to be solved in JavaScript. 我遇到两个有关交易的问题,需要用JavaScript解决。

Let's say some transaction requests are going to happen after (2seconds, 5seconds, 6seconds, 9seconds, 12seconds, 16seconds...). 假设某些事务请求将在之后(2秒,5秒,6秒,9秒,12秒,16秒...)发生。 I need to write a simple function that makes sure there is at least a 5sec interval between two requests and ignores others. 我需要编写一个简单的函数,以确保两个请求之间至少间隔5秒,并且忽略其他请求。 So in this example, only transactions at 2seconds, 9seconds, and 16seconds are accepted. 因此,在此示例中,仅接受2秒,9秒和16秒的事务。 Another problem I had is that writing a function that only accepts a request at 0s, 5s, 10s, 15s...whatever is closer to the 5s mark and ignore others. 我遇到的另一个问题是编写一个仅接受0s,5s,10s,15s ...的请求的函数,无论哪个更接近5s标记而忽略其他标记。 This time, the function should accept transactions at 2sec, 5sec, 9sec, and 16sec and ignore others. 这一次,该函数应在2秒,5秒,9秒和16秒时接受事务,而忽略其他事务。

` `

setTimeout(transaction(2), 2000);
setTimeout(transaction(5), 5000);
setTimeout(transaction(6), 6000);
setTimeout(transaction(9), 9000);
setTimeout(transaction(12), 12000);
setTimeout(transaction(16), 16000);
//first problem: print 2,9,16 
//second problem: print 2,5,9,16

` `

I have an idea to solve these two problems using setTimeout/setInterval/Closure but I'm not sure about how I can integrate them together. 我有一个使用setTimeout / setInterval / Closure解决这两个问题的想法,但是我不确定如何将它们集成在一起。

Use RxJS's debounceTime : 使用RxJS的debounceTime

https://www.learnrxjs.io/operators/filtering/debouncetime.html https://www.learnrxjs.io/operators/filtering/debouncetime.html

Discard emitted values that take less than the specified time between output 丢弃发射之间的时间少于指定时间的发射值

let transactions$ = getObservableForTransactions();
transactions$
    .pipe(
        debounceTime( 5000 )
    )
    .subscribe( txn => console.log( txn ) );

Demonstration 示范

For your use-case specifically, I don't know where your "transactions" are coming from - but here's a more fleshed-out example using debounceTime : 具体来说,对于您的用例,我不知道您的“交易”来自哪里-但是,这是一个使用debounceTime的更加充实的示例:

// Step 1: Input:
let input = [
    { value:  2, delay:  2000 },
    { value:  5, delay:  5000 },
    { value:  6, delay:  6000 },
    { value:  9, delay:  9000 },
    { value: 12, delay: 12000 },
    { value: 16, delay: 16000 }
];

// Step 2: Set-up an Observable Subject:
let obs$ = new Subject<number>();

// Set-up an initial anonymous subscriber to print all values (without debouncing):
obs$.subscribe( {
    next: v => console.log( 'value emitted: %o', v );
} );

// Step 3: Set-up the debounced subscriber:
obs$.pipe( debounceTime( 5000 ) )
    .subscribe( { next: v => console.log( 'Debounced value emitted: %o', v ); } );

// Step 4: Spam `setTimeout` to trigger the process:
for( var item in input ) {
    setTimeout( () => obs$.next( item.value, item.delay );
} 

// Step 5: Open your browser console and view the results in real-time.

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

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