Skip to content

Instantly share code, notes, and snippets.

@mauriciosoares
Created October 19, 2015 20:07
Show Gist options
  • Save mauriciosoares/5f7d185e900a23895e24 to your computer and use it in GitHub Desktop.
Save mauriciosoares/5f7d185e900a23895e24 to your computer and use it in GitHub Desktop.
rxjs double click example
let clickStream = Rx.Observable.fromEvent(document.getElementById('link'), 'click');
clickStream
.buffer(clickStream.debounce(250))
.map(list => list.length)
.filter(x => x === 2)
.subscribe(() => {
console.log('doubleclick');
})
@EmanH
Copy link

EmanH commented Nov 3, 2021

This is simpler imho. Will also fire directly directly after x. Only works for double-clicking (not triple click, etc.)

const doubleClickTimespan = 300
Rx.Observable.fromEvent(document.getElementById('link'), 'click')
.pipe(
  startWith(null), pairwise(),
  takeWhile(e => !e[0] || e[0].target === e[1]?.target),
  filter(e => e[0] !== null && e[1].timeStamp - e[0].timeStamp <= doubleClickTimespan),
  map(e => e[0]),
  take(1), repeat()
).subscribe(e => {
  console.log('double clicked', e);
}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment