Last Updated: February 25, 2016
·
1.059K
· phantom

RequestAnimationFrame crossbrowser support

 (function(w) {

    var lT = 0,
        a = ['ms', 'moz', 'webkit', 'o'],
        i = a.length;


    while( --i > -1 && !w.requestAnimationFrame ){
        w.requestAnimationFrame = w[a[i]+'RequestAnimationFrame'];
        w.cancelAnimationFrame = w[a[i]+'CancelAnimationFrame'] 
        || w[a[i]+'CancelRequestAnimationFrame'];
    }

    if ( !w.requestAnimationFrame ){
        w.requestAnimationFrame = function(callback, element) {
            var cT = new Date().getTime(),
            tTC = Math.max(0, 16 - (cT - lT)),
            id = w.setTimeout(function() { callback(cT + tTC); },tTC);
            lT = cT + tTC;
            return id;
        };
    }

    if ( !w.cancelAnimationFrame ){
        w.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
    }

}(window));