Skip to content

Instantly share code, notes, and snippets.

@juggy
Created February 27, 2013 03:23
Show Gist options
  • Save juggy/5044743 to your computer and use it in GitHub Desktop.
Save juggy/5044743 to your computer and use it in GitHub Desktop.
//
// The logic here is the transitionend event fires instantly
// after the transition is done (1ms here). I have seen timeouts
// taking up to 300ms to fire (!) so the main avantage is responsiveness.
//
window.requestTimeout = (function(){
var vendors = 'Webkit Moz O ms Khtml'.split(' '),
supports = (function() {
var div = document.createElement('div'),
len = vendors.length;
return function(prop) {
if ( prop in div.style ) return true;
prop = prop.replace(/^[a-z]/, function(val) {
return val.toUpperCase();
});
while(len--) {
if ( vendors[len] + prop in div.style ) {
return true;
}
}
return false;
};
})();
var gotAnimations = supports("transition"),
$to = $("#timeout" ), callbacks = [], stopped = true;
if(gotAnimations){
$to.on("transitionend webkitTransitionEnd oTransitionEnd otransitionend", function(){
Em.run(function(){
while(callbacks.length > 0){
var cb = callbacks.pop();
cb.call(window);
}
});
if(callbacks.length > 0){
$to.hasClass("in") ? $to.removeClass("in") : $to.addClass("in");
}else{
stopped = true;
}
});
}
return function(callback, t){
if(gotAnimations ){
callbacks.push(callback);
if(stopped){
stopped = false;
$to.hasClass("in") ? $to.removeClass("in") : $to.addClass("in");
}
}else{
window.setTimeout(function(){
Em.run(function(){
callback.call(t || this);
});
}, 0);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment