(function($){ jQuery.fn.timer = function(options,func,funcEnd){ options = $.extend({ i:0, endMin:0, endMax:60, countBack:0, interval:1000 }, options); //======================================================= function _getTime(time){ var d=0,h=0,m=0,s=0; if(time>86400){d=Math.floor(time/86400);time-=86400*d;} if(time>3600){h=Math.floor(time/3600);time-=3600*h;} if(time>60){m=Math.floor(time/60);time-=60*m;} s=time; return { "d":d, "h":h, "m":m, "s":s } } //======================================================= var make = function(){ if(options.i){ var arr=_getTime(options.i); if(arr['m']<10){arr['m']='0'+arr['m'];}if(arr['s']<10){arr['s']='0'+arr['s'];}if(arr['h']<10){arr['h']=('0'+arr['h']);} func(arr['s'],arr['m'],arr['h']); var t=setInterval(function(){ if(!options.countBack){options.i++;}else{options.i--;} arr=_getTime(options.i); if(arr['m']<10){arr['m']='0'+arr['m'];}if(arr['s']<10){arr['s']='0'+arr['s'];}if(arr['h']<10){arr['h']=('0'+arr['h']);} func(arr['s'],arr['m'],arr['h']); if(options.countBack){if(options.i<=options.endMin){clearInterval(t);funcEnd();return;}} else if(options.i>=options.endMax){clearInterval(t);funcEnd();return;} },options.interval); } }; //========================== return this.each(make); // в итоге, метод pluginName вернет текущий объект jQuery обратно }; })(jQuery);