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