chronoman
Version:
Utility class to simplify use of timers created by setTimeout
2 lines • 6.19 kB
JavaScript
!function(e,t){if(void 0===e&&void 0!==window&&(e=window),"object"==typeof module&&module.exports)module.exports=t(require,exports,module);else if("function"==typeof define&&define.amd)define(["require","exports","module"],t);else{var i={exports:e};e.Chronoman=t(function(t){return e[t]},e,i)}}(this,function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};function n(t,e,i){var o;return Array.isArray(t)?(o=t.length)?t[1<o?n(0,o-1):0]:void 0:("number"!=typeof e&&(e=t+1),arguments.length<3&&(i=!0),o=t+(e-t)*Math.random(),i?Math.round(o):o)}e.getRandomValue=n,Array.isArray||(Array.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)});function o(t){var e=this;this._executeTime=[],this._onTimeoutEnd=function(){e._timeoutId=null,e.execute()},t&&"object"===(void 0===t?"undefined":s(t))&&this.setProperties(t)}return o.prototype._period=null,o.prototype.getPeriod=function(){return this._period},o.prototype.setPeriod=function(t){return this._period=t,this},o.prototype.getPeriodValue=function(t){var e,i=t||0===t?t:this.getPeriod();return"function"==typeof i&&(i=i(this)),Array.isArray(i)&&(i=i[(e=this.getExecutionQty())<i.length?e:i.length-1]),i&&"object"===(void 0===i?"undefined":s(i))&&(i=i.list&&i.list.length?n(i.list):("number"!=typeof i.start&&(i.start=0),"number"!=typeof i.end&&(i.end=i.start+1e3),n(i.start,i.end))),i},o.prototype._recurrent=!1,o.prototype.isRecurrent=function(){return this._recurrent},o.prototype.setRecurrent=function(t){return this._recurrent=t,this},o.prototype._repeatQty=0,o.prototype.getRepeatQty=function(){return this._repeatQty},o.prototype.setRepeatQty=function(t){return this._repeatQty=t,this},o.prototype._repeatTest=null,o.prototype.getRepeatTest=function(){return this._repeatTest},o.prototype.setRepeatTest=function(t){return this._repeatTest=t,this},o.prototype._data=null,o.prototype.getData=function(){return this._data},o.prototype.setData=function(t){return this._data=t,this},o.prototype._executionQty=0,o.prototype.getExecutionQty=function(){return this._executionQty},o.prototype.setExecutionQty=function(t){return this._executionQty=0<t?t:0,this},o.prototype._timeoutId=null,o.prototype._setTimeout=function(t){var e=this.getPeriodValue(t);return"number"==typeof e&&(this._timeoutId=setTimeout(this._onTimeoutEnd,e)),this},o.prototype._clearTimeout=function(){return this._timeoutId&&(clearTimeout(this._timeoutId),this._timeoutId=null),this},o.prototype._startTime=null,o.prototype.getStartTime=function(){return this._startTime},o.prototype._stopTime=null,o.prototype.getStopTime=function(){return this._stopTime},o.prototype._executeTime=null,o.prototype.getExecuteTime=function(){return this._executeTime},o.prototype._active=!1,o.prototype.isActive=function(){return this._active},o.prototype.setActive=function(t){return t&&!this._active&&(this._executionQty=0),this._active=t,this._clearTimeout(),t&&(this._executeTime.length=0,this._setTimeout()),this[t?"_startTime":"_stopTime"]=(new Date).getTime(),this},o.prototype.start=function(t){var e=void 0===t?"undefined":s(t);return"number"===e||"function"===e||Array.isArray(t)?this.setPeriod(t):t&&"object"===e&&this.setProperties(t),this.setActive(!0)},o.prototype.stop=function(){return this.setActive(!1)},o.prototype._action=null,o.prototype.getAction=function(){return this._action},o.prototype.setAction=function(t){return this._action=t,this},o.prototype._passToAction=!1,o.prototype.isPassToAction=function(){return this._passToAction},o.prototype.setPassToAction=function(t){return this._passToAction=t,this},o.prototype.setProperties=function(t){return t&&"object"===(void 0===t?"undefined":s(t))&&("action"in t&&this.setAction(t.action),"period"in t&&this.setPeriod(t.period),"recurrent"in t&&this.setRecurrent(t.recurrent),"repeatQty"in t&&this.setRepeatQty(t.repeatQty),"repeatTest"in t&&this.setRepeatTest(t.repeatTest),"active"in t&&this.setActive(t.active),"passToAction"in t&&this.setPassToAction(t.passToAction),"data"in t&&this.setData(t.data)),this},o.prototype.actionResult=void 0,o.prototype.onExecute=null,o.prototype.onExecuteResult=void 0,o.prototype.execute=function(){var t,e,i,o=this.getAction(),n=this.isPassToAction(),r=this.getRepeatTest();return this._clearTimeout(),o&&("function"==typeof o?this.actionResult=n?o(this):o():"function"==typeof o.execute?this.actionResult=n?o.execute(this):o.execute():"function"==typeof o.func&&(this.actionResult=o.func.apply(o.context||o,n?[this]:[]))),"function"==typeof this.onExecute&&(this.onExecuteResult=n?this.onExecute(this):this.onExecute()),this._executionQty++,this._executeTime.push((new Date).getTime()),(t=this.isActive())&&(this.isRecurrent()||this.getRepeatQty()>=this._executionQty||r&&((e=r(this))||0===e)&&(i=void 0===e?"undefined":s(e))&&("number"!==i||0<=e))?this._setTimeout("number"===i||"object"===i||"function"===i?e:null):t&&!this._timeoutId&&this.setActive(!1),this},o.prototype.dispose=function(){this._clearTimeout(),this._action=this.actionResult=this._data=this._executeTime=this._period=this._repeatTest=this.onExecute=this.onExecuteResult=null},o.prototype.toString=function(){var t,e=this.getPeriod(),i=this.getStartTime(),o=this.getStopTime(),n=this.getExecuteTime(),r=n.length,s="";if(r)for(t=0;t<r;t++)s+=(t?"; ":"")+new Date(n[t]);return["Timer: ","active - ",this.isActive(),", period - ","function"==typeof e?"function":e,", recurrent - ",this.isRecurrent(),", repeat qty - ",this.getRepeatQty(),", repeat test - ",this.getRepeatTest()?"specified":"no",", pass to action - ",this.isPassToAction(),", action - ",this.getAction()?"specified":"no",", execution qty - ",this.getExecutionQty(),"number"==typeof i?", start time - "+new Date(i):"",s?", execute time - "+s:"","number"==typeof o?", stop time - "+new Date(o):"",", action result - ",this.actionResult,", onExecuteResult - ",this.onExecuteResult,", data - ",this.getData()].join("")},e.Timer=o,e.default=o,i.exports});
//# sourceMappingURL=chronoman.min.js.map