Created
September 20, 2015 14:42
-
-
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
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
/* | |
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