Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active August 27, 2019 09:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/3faa2a2954f5249f61d9 to your computer and use it in GitHub Desktop.
Save miguelmota/3faa2a2954f5249f61d9 to your computer and use it in GitHub Desktop.
D3 callback when all transitions are done
function endAll(transition, callback) {
var n = 0
transition.each(() => ++n)
.each('end', () => (!--n && callback.apply(this, arguments)))
}
// Usage
// d3.selectAll('g').transition().call(endAll, allDone)
@kashesandr
Copy link

kashesandr commented Sep 22, 2017

Here is an updated version

  • fixed: If the selection contains zero elements, the callback will never fire.
  • added: an error throwing if no callback

https://stackoverflow.com/a/20773846/1061438

@mscheper
Copy link

It's so infuriating that this isn't built into D3. While clever, this is messy boilerplate code that I'm getting tired of writing over and over again. Synchronising the enter() and exit() is tricky, too.

@phocks
Copy link

phocks commented Apr 23, 2019

Seems to be built in now, with Promises.

https://github.com/d3/d3-transition#transition_end

Looks like we can do something like this:

d3.selectAll("g")
  .transition("name")
  .duration(1000)
  .end()
  .then(() => {
    // do something
  });

@nrmtmt
Copy link

nrmtmt commented Aug 27, 2019

@phocks you literally saved my life

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