﻿jQuery.fn.extend({everyTime:function(d,a,c,b,e){return this.each(function(){jQuery.timer.add(this,d,a,c,b,e)})},oneTime:function(d,a,c){return this.each(function(){jQuery.timer.add(this,d,a,c,1)})},stopTime:function(d,a){return this.each(function(){jQuery.timer.remove(this,d,a)})}});jQuery.extend({timer:{global:[],guid:1,dataKey:"jQuery.timer",regex:/^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,powers:{'ms':1,'cs':10,'ds':100,'s':1000,'das':10000,'hs':100000,'ks':1000000},timeParse:function(d){if(d==undefined||d==null)return null;var a=this.regex.exec(jQuery.trim(d.toString()));if(a[2]){var c=parseFloat(a[1]);var b=this.powers[a[2]]||1;return c*b}else{return d}},add:function(d,a,c,b,e,f){var h=0;if(jQuery.isFunction(c)){if(!e)e=b;b=c;c=a}a=jQuery.timer.timeParse(a);if(typeof a!='number'||isNaN(a)||a<=0)return;if(e&&e.constructor!=Number){f=!!e;e=0}e=e||0;f=f||false;var g=jQuery.data(d,this.dataKey)||jQuery.data(d,this.dataKey,{});if(!g[c])g[c]={};b.timerID=b.timerID||this.guid++;var i=function(){if(f&&this.inProgress)return;this.inProgress=true;if((++h>e&&e!==0)||b.call(d,h)===false)jQuery.timer.remove(d,c,b);this.inProgress=false};i.timerID=b.timerID;if(!g[c][b.timerID])g[c][b.timerID]=window.setInterval(i,a);this.global.push(d)},remove:function(d,a,c){var b=jQuery.data(d,this.dataKey),e;if(b){if(!a){for(a in b)this.remove(d,a,c)}else if(b[a]){if(c){if(c.timerID){window.clearInterval(b[a][c.timerID]);delete b[a][c.timerID]}}else{for(var c in b[a]){window.clearInterval(b[a][c]);delete b[a][c]}}for(e in b[a])break;if(!e){e=null;delete b[a]}}for(e in b)break;if(!e)jQuery.removeData(d,this.dataKey)}}}});jQuery(window).bind("unload",function(){jQuery.each(jQuery.timer.global,function(d,a){jQuery.timer.remove(a)})});
