requestAnimationFrame

modify from newer Paul Irish's Polyfill

JavaScript

// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating

// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel

// little saving...

(function() {
    var w = window,
        a = "ancel",
        b = "equest",
        c = "c" + a,
        C = "C" + a,
        r = "r" + b,
        R = "R" + b,
        af = "AnimationFrame",
        x = 0,
        lastTime = 0,
        v = ["ms", "moz", "webkit", "o"],
        vl = v.length;

    for (; x < vl && !w[r + af]; ++x) {
        w[r + af] = w[v[x] + R + af];
        w[c + af] = w[v[x] + C + af] || w[v[x] + C + R + af];
    }

    if (!w[r + af]) {
        w[r + af] = function(callback, element) {
            var currTime = +new Date(),
                timeToCall = Math.max(0, 16 - (currTime - lastTime)),
                id = w.setTimeout(function() {
                    callback(currTime + timeToCall);
                }, timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };
    }

    if (!w[c + af]) {
        w[c + af] = function(id) {
            clearTimeout(id);
        };
    }
}());