Javascript 从jQuery中删除未使用的变量

Javascript 从jQuery中删除未使用的变量,javascript,jquery,variables,Javascript,Jquery,Variables,我有一个jQuery脚本,我已经收紧并缩小了它。我不喜欢链接到需要HTTP请求加上40-90kb客户端下载的CDNjQuery脚本 我听说新的Google闭包编译器可以调整为从jQuery中删除未使用的变量以减小大小。我理解像google一样使用CDN链接的好处,但因为我只在一个页面上使用此代码,所以我更愿意从jQuery中删除未使用的变量并自己托管它 我正在使用jQuery 1.5.2,下面是我的脚本: (function(a){a.fn.countDown=function(b){confi

我有一个jQuery脚本,我已经收紧并缩小了它。我不喜欢链接到需要HTTP请求加上40-90kb客户端下载的CDNjQuery脚本

我听说新的Google闭包编译器可以调整为从jQuery中删除未使用的变量以减小大小。我理解像google一样使用CDN链接的好处,但因为我只在一个页面上使用此代码,所以我更愿意从jQuery中删除未使用的变量并自己托管它

我正在使用jQuery 1.5.2,下面是我的脚本:

(function(a){a.fn.countDown=function(b){config={};a.extend(config,b);diffSecs=this.setCountDown(config);config.onComplete&&a.data(a(this)[0],"callback",config.onComplete);config.omitWeeks&&a.data(a(this)[0],"omitWeeks",config.omitWeeks);a("#"+a(this).attr("id")+" .digit").html('<div class="top"></div><div class="bottom"></div>');a(this).doCountDown(a(this).attr("id"),diffSecs,500);return this};a.fn.stopCountDown=function(){clearTimeout(a.data(this[0],"timer"))};a.fn.startCountDown=function(){this.doCountDown(a(this).attr("id"), a.data(this[0],"diffSecs"),500)};a.fn.setCountDown=function(b){var c=new Date;b.targetDate?c=new Date(b.targetDate.month+"/"+b.targetDate.day+"/"+b.targetDate.year+" "+b.targetDate.hour+":"+b.targetDate.min+":"+b.targetDate.sec+(b.targetDate.utc?" UTC":"")):b.targetOffset&&(c.setFullYear(b.targetOffset.year+c.getFullYear()),c.setMonth(b.targetOffset.month+c.getMonth()),c.setDate(b.targetOffset.day+c.getDate()),c.setHours(b.targetOffset.hour+c.getHours()),c.setMinutes(b.targetOffset.min+c.getMinutes()), c.setSeconds(b.targetOffset.sec+c.getSeconds()));diffSecs=Math.floor((c.valueOf()-(new Date).valueOf())/1E3);a.data(this[0],"diffSecs",diffSecs);return diffSecs};a.fn.doCountDown=function(b,c,d){$this=a("#"+b);0>=c&&(c=0,a.data($this[0],"timer")&&clearTimeout(a.data($this[0],"timer")));secs=c%60;mins=Math.floor(c/60)%60;hours=Math.floor(c/60/60)%24;days=!0==a.data($this[0],"omitWeeks")?Math.floor(c/60/60/24):Math.floor(c/60/60/24)%7;weeks=Math.floor(c/60/60/24/7);$this.dashChangeTo(b,"seconds_dash", secs,d?d:800);$this.dashChangeTo(b,"minutes_dash",mins,d?d:1200);$this.dashChangeTo(b,"hours_dash",hours,d?d:1200);$this.dashChangeTo(b,"days_dash",days,d?d:1200);$this.dashChangeTo(b,"weeks_dash",weeks,d?d:1200);a.data($this[0],"diffSecs",c);0<c?(e=$this,t=setTimeout(function(){e.doCountDown(b,c-1)},1E3),a.data(e[0],"timer",t)):(cb=a.data($this[0],"callback"))&&a.data($this[0],"callback")()};a.fn.dashChangeTo=function(b,c,d,m){$this=a("#"+b);for(b=$this.find("."+c+" .digit").length-1;0<=b;b--){var j= d%10,d=(d-j)/10;$this.digitChangeTo("#"+$this.attr("id")+" ."+c+" .digit:eq("+b+")",j,m)}};a.fn.digitChangeTo=function(b,c,d){d||(d=800);a(b+" div.top").html()!=c+""&&(a(b+" div.top").css({display:"none"}),a(b+" div.top").html(c?c:"0").slideDown(d),a(b+" div.bottom").animate({height:""},d,function(){a(b+" div.bottom").html(a(b+" div.top").html());a(b+" div.bottom").css({display:"block",height:""});a(b+" div.top").hide().slideUp(10)}))}})(jQuery); jQuery(document).ready(function(){$("#countdown").countDown({targetDate:{day:7,month:5,year:2012,hour:8,min:0,sec:0,utc:!0}})});$(document).ready(function(){$("#xtreme_slider").tinycarousel()}); (function(a){function b(c,b){function m(){b.controls&&0<n.length&&0<o.length&&(n.click(function(){g.move(-1);return!1}),o.click(function(){g.move(1);return!1}));b.interval&&c.hover(g.stop,g.start);b.pager&&0<p.length&&a("a",p).click(x)}function j(){b.controls&&(n.toggleClass("disable",!(0<f)),o.toggleClass("disable",!(f+1<h)));if(b.pager){var c=a(".pagenum",p);c.removeClass("active");a(c[f]).addClass("active")}}function x(){a(this).hasClass("pagenum")&&g.move(parseInt(this.rel),!0);return!1}function v(){b.interval&& !q&&(clearTimeout(r),r=setTimeout(function(){f=f+1==h?-1:f;s=f+1==h?!1:0==f?!0:s;g.move(s?1:-1)},b.intervaltime))}var g=this,w=a(".viewport:first",c),u=a(".overview:first",c),i=u.children(),o=a(".next:first",c),n=a(".prev:first",c),p=a(".pager:first",c),k,h,f,r,q,s=!0,l="x"==b.axis;this.stop=function(){clearTimeout(r);q=!0};this.start=function(){q=!1;v()};this.move=function(a,c){f=c?a:f+=a;if(-1<f&&f<h){var g={};g[l?"left":"top"]=-(f*k*b.display);u.animate(g,{queue:!1,duration:b.animation?b.duration: 0,complete:function(){"function"==typeof b.callback&&b.callback.call(this,i[f],f)}});j();v()}};return function(){k=l?a(i[0]).outerWidth(!0):a(i[0]).outerHeight(!0);var c=Math.ceil((l?w.outerWidth():w.outerHeight())/(k*b.display)-1);h=Math.max(1,Math.ceil(i.length/b.display)-c);f=Math.min(h,Math.max(1,b.start))-2;u.css(l?"width":"height",k*i.length);g.move(1);m();return g}()}a.tiny=a.tiny||{};a.tiny.carousel={options:{start:1,display:1,axis:"x",controls:!1,pager:!0,interval:!0,intervaltime:5E3,rewind:!1, animation:!0,duration:1E3,callback:null}};a.fn.tinycarousel=function(c){c=a.extend({},a.tiny.carousel.options,c);this.each(function(){a(this).data("tcl",new b(a(this),c))});return this};a.fn.tinycarousel_start=function(){a(this).data("tcl").start()};a.fn.tinycarousel_stop=function(){a(this).data("tcl").stop()};a.fn.tinycarousel_move=function(b){a(this).data("tcl").move(b-1,!0)}})(jQuery);

有人可以根据我的脚本从jQuery中删除未使用的代码吗?

您可以通过修改构建脚本来尝试自定义jQuery构建。如果可以,你也可以试试。这个库非常轻量级,并且具有与jQuery兼容的API

此外,.

可以轻松地将代码在simple上减少约59%,在advanced上减少约65%。我建议在生产服务器上使用它们之前,先试用这两种方法,并检查它们是否都能工作,但这似乎正是您想要的


此外,在编译时,请确保使用顶部的选择框使用jQuery库进行编译。

虽然您提到了一些可能的问题,但您从未明确说明您的问题是什么?此外,Google Closure虽然有用,但对于从一开始就没有考虑过它的代码,可能不是最好的方法,如果不清楚,我很抱歉。我现在用一个更清楚的问题编辑了这篇文章@ZenMaster是真的,但我正在寻找任何可以从jQuery中消除未使用的变量的服务。正如您可以看到我的脚本一样,您将看到,如果它几乎不使用约20%的jQuery变量!如果可能的话,我想节省约80%.还有什么更糟/需要更长的时间?自定义构建jQuery部件?或者加载一次,让缓存在接下来的几个请求中为您解决32kb的问题?这对我来说太复杂了!: