Last active
August 27, 2019 09:23
-
-
Save miguelmota/3faa2a2954f5249f61d9 to your computer and use it in GitHub Desktop.
D3 callback when all transitions are done
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Here is an updated version
- fixed: If the selection contains zero elements, the callback will never fire.
- added: an error throwing if no callback
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.
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
});
@phocks you literally saved my life
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clever! Found at http://stackoverflow.com/questions/10692100/invoke-a-callback-at-the-end-of-a-transition#comment65147208_10692220