timer.min.js 4.6 KB

1
  1. /*! timer.jquery 0.4.14 2016-05-14*/!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],b):b(a.jQuery)}(this,function(a){function b(b){var c=b.element;a(c).data("intr",setInterval(d.bind(b),b.options.updateFrequency)),a(c).data("isTimerRunning",!0)}function c(b){clearInterval(a(b.element).data("intr")),a(b.element).data("isTimerRunning",!1)}function d(){a(this.element).data("totalSeconds",g()-a(this.element).data("startTime")),e(this),a(this.element).data("duration")&&a(this.element).data("totalSeconds")%a(this.element).data("duration")===0&&(this.options.repeat||(a(this.element).data("duration",null),this.options.duration=null),this.options.countdown&&(c(this),this.options.countdown=!1,a(this.element).data("state",r)),this.options.callback())}function e(b){var c=b.element,d=a(c).data("totalSeconds");b.options.countdown&&a(c).data("duration")>0&&(d=a(c).data("duration")-a(c).data("totalSeconds")),a(c)[q](i(d,b)),a(c).data("seconds",d)}function f(b){var c=b.element;a(c).on("focus",function(){l(b)}),a(c).on("blur",function(){var d,e=a(c)[q]();e.indexOf("sec")>0?a(c).data("totalSeconds",Number(e.replace(/\ssec/g,""))):e.indexOf("min")>0?(e=e.replace(/\smin/g,""),d=e.split(":"),a(c).data("totalSeconds",Number(60*d[0])+Number(d[1]))):e.match(/\d{1,2}:\d{2}:\d{2}/)&&(d=e.split(":"),a(c).data("totalSeconds",Number(3600*d[0])+Number(60*d[1])+Number(d[2]))),m(b)})}function g(){return Math.round((new Date).getTime()/1e3)}function h(a){var b,c=0,d=Math.floor(a/60),e=d;return a>=3600&&(c=Math.floor(a/3600)),a>=3600&&(e=Math.floor(a%3600/60)),10>e&&c>0&&(e="0"+e),b=a%60,10>b&&(e>0||c>0)&&(b="0"+b),{hours:c,minutes:e,totalMinutes:d,seconds:b,totalSeconds:a}}function i(a,b){var c="",d=h(a);if(b.options.format){var e=[{identifier:"%h",value:d.hours,pad:!1},{identifier:"%m",value:d.minutes,pad:!1},{identifier:"%s",value:d.seconds,pad:!1},{identifier:"%g",value:d.totalMinutes,pad:!1},{identifier:"%t",value:d.totalSeconds,pad:!1},{identifier:"%H",value:parseInt(d.hours),pad:!0},{identifier:"%M",value:parseInt(d.minutes),pad:!0},{identifier:"%S",value:parseInt(d.seconds),pad:!0},{identifier:"%G",value:parseInt(d.totalMinutes),pad:!0},{identifier:"%T",value:parseInt(d.totalSeconds),pad:!0}];c=b.options.format,e.forEach(function(a){c=c.replace(new RegExp(a.identifier.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1"),"g"),a.pad&&a.value<10?"0"+a.value:a.value)})}else c=d.hours?d.hours+":"+d.minutes+":"+d.seconds:d.minutes?d.minutes+":"+d.seconds+" min":d.seconds+" sec";return c}function j(a){if(!isNaN(Number(a)))return a;var b=a.match(/\d{1,2}h/),c=a.match(/\d{1,2}m/),d=a.match(/\d{1,2}s/),e=0;return a=a.toLowerCase(),b&&(e+=3600*Number(b[0].replace("h",""))),c&&(e+=60*Number(c[0].replace("m",""))),d&&(e+=Number(d[0].replace("s",""))),e}function k(c){var d=c.element;a(d).data("isTimerRunning")||(e(c),b(c),a(d).data("state",s),c.options.startTimer.bind(c).call())}function l(b){var d=b.element;a(d).data("isTimerRunning")&&(c(b),a(d).data("state",t),b.options.pauseTimer.bind(b).call())}function m(c){var d=c.element;a(d).data("isTimerRunning")||(a(d).data("startTime",g()-a(d).data("totalSeconds")),b(c),a(d).data("state",s),c.options.resumeTimer.bind(c).call())}function n(a){o(a),k(a)}function o(b){var d=b.element;c(b),b.options.removeTimer.bind(b).call(),a(d).data("isTimerRunning",null),a(d).data("plugin_"+v,null),a(d).data("seconds",null),a(d).data("totalSeconds",null),a(d).data("state",null),a(d)[q]("")}var p={seconds:0,editable:!1,restart:!1,duration:null,callback:function(){alert("Time up!")},startTimer:function(){},pauseTimer:function(){},resumeTimer:function(){},resetTimer:function(){},removeTimer:function(){},repeat:!1,countdown:!1,format:null,updateFrequency:1e3,state:"running"},q="html",r="stopped",s="running",t="paused",u=function(b,c){var d;this.options=p=a.extend(this.options,p,c),this.element=b,a(b).data("totalSeconds",p.seconds),a(b).data("startTime",g()-a(b).data("totalSeconds")),a(b).data("seconds",a(b).data("totalSeconds")),a(b).data("state",r),d=a(b).prop("tagName").toLowerCase(),"input"!==d&&"textarea"!==d||(q="val"),this.options.duration&&(a(b).data("duration",j(this.options.duration)),this.options.duration=j(this.options.duration)),this.options.editable&&f(this)};u.prototype={start:function(){k(this)},pause:function(){l(this)},resume:function(){m(this)},reset:function(){n(this)},remove:function(){o(this)}};var v="timer";a.fn[v]=function(b){return b=b||"start",this.each(function(){a.data(this,"plugin_"+v)instanceof u||a.data(this,"plugin_"+v,new u(this,b));var c=a.data(this,"plugin_"+v);"string"==typeof b&&"function"==typeof c[b]&&c[b].call(c),"object"==typeof b&&(c.options.state===s?c.start.call(c):e(c))})}});
粤ICP备19079148号