Another variation that is suitable for detecting a pure function (rather than a purely pragmatic solution), but more effective:
var transitionendNames = [ 'transitionend', 'webkitTransitionEnd', 'MozTransitionEnd', 'oTransitionEnd' ]; /** * Helper function to bind to the correct transitionend event * @param {function} callback The function to call when the event fires */ var transitionend = function(elem, callback) { var handler = function(e) { //console.log('transitionend == '+e.type); // test in case multiple were registered before change if (transitionendNames) { // store the actual name var transitionendName = e.type; // every other time, bind only to actual event transitionend = function(elem, callback) { elem.addEventListener(transitionendName, callback, false); }; // flag for any others transitionendNames = null; } return callback.call(elem, e); }; // until an event has been triggered bind them all for (var i=0, len=transitionendNames.length; i<len; i++) { elem.addEventListener(transitionendNames[i], handler, false); } };
source share