求高手优化js代码

a3572950 2011-09-09 03:57:16
function changenumber(id,x){
this.id = id;
this.n = x;
this.gn = 0;
this.sn = 0;
this.bn = 0;
this.qn = 0;
this.wn = 0;
this.swn = 0;

this.g=function(o,id,n){
var s_top = $("#t_6_"+id).scrollTop();
if (s_top>479)
{
o.s(o,id,48);
s_top=0;
}
if(o.gn++<n)
{
$("#t_6_"+id).scrollTop(s_top+1);
setTimeout(function(){o.g(o,id,n);},1);
}
else
o.gn=0;
$("#x_6_"+id).html($("#t_6_"+id).scrollTop());
}
this.s=function(o,id,n){
var s_top = $("#t_5_"+id).scrollTop();
if (s_top>=479)
{
o.b(o,id,48);
s_top=-1;
}
if(o.sn++<n)
{
$("#t_5_"+id).scrollTop(s_top+1);
setTimeout(function(){o.s(o,id,n);},1);
}
else
o.sn = 0;
$("#x_5_"+id).html($("#t_5_"+id).scrollTop());
}
this.b=function(o,id,n){
var s_top = $("#t_4_"+id).scrollTop();
if (s_top>=480)
{
o.q(o,id,48);
s_top=0;
}
if(o.bn++<n)
{
$("#t_4_"+id).scrollTop(s_top+1);
setTimeout(function(){o.b(o,id,n);},1);
}
else
o.bn = 0;
// $("#x_4_"+id).html($("#t_4_"+id).scrollTop());
}
this.q=function(o,id,n){
var s_top = $("#t_3_"+id).scrollTop();
if (s_top>=480)
{
o.w(o,id,48);
s_top=0;
}
if(o.qn++<n)
{
$("#t_3_"+id).scrollTop(s_top+1);
setTimeout(function(){o.q(o,id,n);},1);
}
else
o.qn=0;
// $("#x_3_"+id).html($("#t_3_"+id).scrollTop());
}
this.w=function(id,n){
var s_top = $("#t_2_"+id).scrollTop();
if (s_top>=480)
{
o.sw(o,id,48);
s_top=0;
}
if(o.wn++<n)
{
$("#t_2_"+id).scrollTop(s_top+1);
setTimeout(function(){o.w(o,id,n);},1);
}
else
o.wn = 0;
// $("#x_2_"+id).html($("#t_2_"+id).scrollTop());
}
this.sw = function(o,id,n){
var s_top = $("#t_1_"+id).scrollTop();
if (s_top>=480)
{
s_top=0;
}
if(o.swn++<n)
{
$("#t_1_"+id).scrollTop(s_top+1);
setTimeout(function(){o.sw(o,id,n);},1);
}
else
o.swn=0;
// $("#x_1_"+id).html($("#t_1_"+id).scrollTop());
}
this.run = function(o){
o.n*=48;
setTimeout(function(){o.g(o,o.id,o.n);},1);
}

}


调用部分

$(document).ready(function(){
var x1 =new changenumber(37,40);
var x2 =new changenumber(36,30);
setTimeout(function(){x1.run(x1);},50);
setTimeout(function(){x2.run(x2);},99);
})



<p class="counts">
<span id="t_1_37" class="t_1 t"><img src="images/09.jpg"></span>
<span id="t_2_37" class="t_2 t"><img src="images/09.jpg"></span>
<span id="t_3_37" class="t_3 t"><img src="images/09.jpg"></span>
<span id="t_4_37" class="t_4 t"><img src="images/09.jpg"></span>
<span id="t_5_37" class="t_5 t"><img src="images/09.jpg"></span>
<span id="t_6_37" class="t_6 t"><img src="images/09.jpg"></span>
</p>
<p class="counts">
<span id="t_1_36" class="t_1 t"><img src="images/09.jpg"></span>
<span id="t_2_36" class="t_2 t"><img src="images/09.jpg"></span>
<span id="t_3_36" class="t_3 t"><img src="images/09.jpg"></span>
<span id="t_4_36" class="t_4 t"><img src="images/09.jpg"></span>
<span id="t_5_36" class="t_5 t"><img src="images/09.jpg"></span>
<span id="t_6_36" class="t_6 t"><img src="images/09.jpg"></span>
</p>


IE里速度很不理想 求高手优化
...全文
190 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
a3572950 2011-09-16
  • 打赏
  • 举报
回复
a3572950 2011-09-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liangws 的回复:]

this.g=function(o,id,n){
var s_top = $("#t_6_"+id).scrollTop();
if (s_top>479)
{
o.s(o,id,48);
s_top=0;
}
if(o.gn++<n)
……
[/Quote]
这样的话也速度提高应该不会有太明显吧
a3572950 2011-09-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 relax19830426 的回复:]

JS类的方法(虽然JS没有真正意义上的类)一般使用对象原型来定义会比较节省资源
例如你的这个类
function changenumber(id,x){
........
this.g=function(o,id,n){
....
}
.....
}
优化层面来说应该这样写
function changenumber(id,x)……
[/Quote]

本来是想把进位都写成一个函数的 不过发现会有一个像素的差别 所以就写成这样了
liangws 2011-09-16
  • 打赏
  • 举报
回复
this.g=function(o,id,n){
var s_top = $("#t_6_"+id).scrollTop();
if (s_top>479)
{
o.s(o,id,48);
s_top=0;
}
if(o.gn++<n)
{
$("#t_6_"+id).scrollTop(s_top+1);
setTimeout(function(){o.g(o,id,n);},1);
}
else
o.gn=0;
$("#x_6_"+id).html($("#t_6_"+id).scrollTop());
}

$("#t_6_"+id)使用了3次,,LZ可以讲$("#t_6_"+id)放到一个局部变量中使用
liangws 2011-09-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 relax19830426 的回复:]

JS类的方法(虽然JS没有真正意义上的类)一般使用对象原型来定义会比较节省资源
例如你的这个类
function changenumber(id,x){
........
this.g=function(o,id,n){
....
}
.....
}
优化层面来说应该这样写
function changenumber(id,x)……
[/Quote]

查找成员变量明显比查找原型链速度来得快。。。原型继承是用空间换取时间,所以追求速度的话,还是LZ快点
relax19830426 2011-09-15
  • 打赏
  • 举报
回复
JS类的方法(虽然JS没有真正意义上的类)一般使用对象原型来定义会比较节省资源
例如你的这个类
function changenumber(id,x){
........
this.g=function(o,id,n){
....
}
.....
}
优化层面来说应该这样写
function changenumber(id,x){
......
}
//对象原型的方法
changenumber.prototype.g=function(o,id,n){
......
}
其他优化是逻辑层面上的 原则就是把共有的程序过程提取出来 写成函数调用 另外有时间可以注意下扩展性以及容错
Gemini Dean 2011-09-15
  • 打赏
  • 举报
回复
帮顶!
a3572950 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dxx1988 的回复:]

都不知这是干啥用的
[/Quote]

js控制显示数字的图片滚动 每一个低位的滚动10个数就进位 高位的滚动一位
Exodia 2011-09-09
  • 打赏
  • 举报
回复
都不知这是干啥用的

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧