Skip to content

Instantly share code, notes, and snippets.

@bumbu
Created September 20, 2015 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bumbu/dd5cb25a8762a0cab855 to your computer and use it in GitHub Desktop.
Save bumbu/dd5cb25a8762a0cab855 to your computer and use it in GitHub Desktop.
A function for triggering a callback at the end of D3.js transitions
/*
Accepts one or more transitions and a callback as last argument
Example:
```
var transition1 = d3.selectAll('g').transition().duration(500)
, transition2 = d3.selectAll('circle').transition().duration(400)
, callback = function() {console.log('done')}
onD3TransitionsEnd(transition1, transition2, callback)
```
*/
function onD3TransitionsEnd() {
var args = Array.prototype.slice.call(arguments)
, selectionCount = 0
, cb = args[args.length - 1]
for (var i = 0; i < args.length - 1; i++) {
selectionCount += args[i].length
args[i].each('end', function(){
selectionCount -= 1
selectionCount === 0 && cb()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment