UNPKG

1.07 kBJavaScriptView Raw
1!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):e.microFps=o()}(this,function(){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};return function(r){var f=1<arguments.length&&void 0!==arguments[1]?arguments[1]:1;if("function"!=typeof r)throw new Error("Callback is not a function");if("number"!=typeof f||isNaN(f)||f<0||f===1/0)throw new Error("refreshRate should be a positive number! e.g. 2 (fps)");var i=-1,a=0,p=void 0,u=0,c=0,s="object"===("undefined"==typeof performance?"undefined":e(performance))&&"now"in performance?performance.now.bind(performance):Date.now.bind(Date);return function(){if(a<=i){var e=s();void 0===p&&(p=e);var o=e-p;if(0<o){var n=0<i?1e3/(o/i):0;c=Math.abs(u-n),0<f?(a=.5*a+n/f*.5)<0&&(a=0):a=0;var t={fps:n,jitter:c,elapsed:o,frames:i,trigger:a};p=e,u=n,i=0,r(t)}else a*=2}i++}}});