2024-02-14 13:02:11 -05:00
|
|
|
var t,e,n=0,i=0,r=0,o=1e3,l=0,a=0,u=0,s="object"===typeof performance&&performance.now?performance:Date,c="object"===typeof window&&window.requestAnimationFrame?window.requestAnimationFrame.bind(window):function(t){setTimeout(t,17)};function now(){return a||(c(clearNow),a=s.now()+u)}function clearNow(){a=0}function Timer(){this._call=this._time=this._next=null}Timer.prototype=timer.prototype={constructor:Timer,restart:function(n,i,r){if("function"!==typeof n)throw new TypeError("callback is not a function");r=(null==r?now():+r)+(null==i?0:+i);if(!this._next&&e!==this){e?e._next=this:t=this;e=this}this._call=n;this._time=r;sleep()},stop:function(){if(this._call){this._call=null;this._time=Infinity;sleep()}}};function timer(t,e,n){var i=new Timer;i.restart(t,e,n);return i}function timerFlush(){now();++n;var e,i=t;while(i){(e=a-i._time)>=0&&i._call.call(void 0,e);i=i._next}--n}function wake(){a=(l=s.now())+u;n=i=0;try{timerFlush()}finally{n=0;nap();a=0}}function poke(){var t=s.now(),e=t-l;e>o&&(u-=e,l=t)}function nap(){var n,i,r=t,o=Infinity;while(r)if(r._call){o>r._time&&(o=r._time);n=r,r=r._next}else{i=r._next,r._next=null;r=n?n._next=i:t=i}e=n;sleep(o)}function sleep(t){if(!n){i&&(i=clearTimeout(i));var e=t-a;if(e>24){t<Infinity&&(i=setTimeout(wake,t-s.now()-u));r&&(r=clearInterval(r))}else{r||(l=s.now(),r=setInterval(poke,o));n=1,c(wake)}}}function timeout(t,e,n){var i=new Timer;e=null==e?0:+e;i.restart((n=>{i.stop();t(n+e)}),e,n);return i}function interval(t,e,n){var i=new Timer,r=e;if(null==e)return i.restart(t,e,n),i;i._restart=i.restart;i.restart=function(t,e,n){e=+e,n=null==n?now():+n;i._restart((function tick(o){o+=r;i._restart(tick,r+=e,n);t(o)}),e,n)};i.restart(t,e,n);return i}export{interval,now,timeout,timer,timerFlush};
|