﻿jQuery.fn.extend({everyTime:function(a,c,e,d,b){return this.each(function(){jQuery.timer.add(this,a,c,e,d,b)})},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.event.special;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 d=parseFloat(b[1]),c=this.powers[b[2]]||1;return d*c}else return a},add:function(e,d,c,b,a,g){var h=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(a&&a.constructor!=Number){g=!!a;a=0}a=a||0;g=g||false;var f=jQuery.data(e,this.dataKey)||jQuery.data(e,this.dataKey,{});if(!f[c])f[c]={};b.timerID=b.timerID||this.guid++;var i=function(){if(g&&this.inProgress)return;this.inProgress=true;if(++h>a&&a!==0||b.call(e,h)===false)jQuery.timer.remove(e,c,b);this.inProgress=false};i.timerID=b.timerID;if(!f[c][b.timerID])f[c][b.timerID]=window.setInterval(i,d);this.global.push(e)},remove:function(e,b,c){var a=jQuery.data(e,this.dataKey),d;if(a){if(!b)for(b in a)this.remove(e,b,c);else if(a[b]){if(c){if(c.timerID){window.clearInterval(a[b][c.timerID]);delete a[b][c.timerID]}}else for(var c in a[b]){window.clearInterval(a[b][c]);delete a[b][c]}for(d in a[b])break;if(!d){d=null;delete a[b]}}for(d in a)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)})})