All files / src/polyfill requestAnimatiionFrame.ts

100% Statements 23/23
87.5% Branches 7/8
100% Functions 3/3
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46            1x 1x   1x   1x 1x     1x 4x 4x       1x 1x   33x   33x 33x 33x   33x       1x 1x 1x       1x 1x     1x  
// 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
 
// MIT license
const rAF: any = "requestAnimationFrame";
const cAF: any = "cancelAnimationFrame";
 
let _this: any = this || {};
 
const vendors: string[] = ['ms', 'moz', 'webkit', 'o'];
let lastTime: number = 0;
let x: number;
 
for (x = 0; x < vendors.length && !_this[rAF]; ++x) {
    _this[rAF] = _this[vendors[x] + 'RequestAnimationFrame'];
    _this[cAF] = _this[vendors[x] + 'CancelAnimationFrame']
        || _this[vendors[x] + 'CancelRequestAnimationFrame'];
}
 
if (!_this[rAF]) {
    _this[rAF] = function ( callback: Function ) {
 
        const currTime = Date.now(),
        
        timeToCall = Math.max(0, 16 - (currTime - lastTime)),
        id = setTimeout(function () { callback(currTime + timeToCall); }, timeToCall);
        lastTime = currTime + timeToCall;
 
        return id;
    };
}
 
if (!_this[cAF]) {
    _this[cAF] = function ( id: number ) {
        clearTimeout( id );
    };
}
 
let requestAnimationFrame = _this[ rAF ];
let cancelAnimationFrame = _this[ cAF ];
 
 
export { requestAnimationFrame, cancelAnimationFrame };