首页 新闻 会员 周边

Jquery Each优化?急!急!急!

0
悬赏园豆:50 [已解决问题] 解决于 2013-05-30 21:35

//总重量
$(function () {
$(".weight").each(function () {
var id = $(this).attr("itemid");
var shopid = $(this).attr("shopid");
$(this).change(function () {
var sum = 0;
$(".weight").each(function () {退出
if ($(this).val().length != 0) {
var itid = $(this).attr("itemid");
var qty = $(".auditqty[itemid=" + itid + "]").val();
sum += parseFloat($(this).val() * qty);
//alert(qty);
}
});
$("#sumWeight").text(sum.toFixed(3));
//回传中中运费
cncnFreightFunction(id, shopid);
//回传中泰运费
// cnthtypechange(id);

$(".weight").each(function () {
var skuId = $(this).attr("itemid");
cnthtypechange(skuId);
})

$(".sumGoodWeight").each(function () {
var id = $(this).attr("itemid");
$(this).text(Number(($(".auditqty[itemid=" + id + "]").val()) * ($(".weight[itemid=" + id + "]").val())).toFixed(3));
$(".sumGoodVolumn[itemid=" + id + "]").text((($(".auditqty[itemid=" + id + "]").val()) * ($(".volumes[itemid=" + id + "]").val())).toFixed(3));
});
ThThFreight();
});
});
});

 

$(".weight").each(function () {
var skuId = $(this).attr("itemid");
cnthtypechange(skuId);
})这里能否在优化,因为一但商品很多的时候严重影响到速度的问题!

茶饭不思的主页 茶饭不思 | 初学一级 | 园豆:123
提问于:2013-05-19 18:02
< >
分享
最佳答案
0

影响相应速度的关键点在于cnthtypechange(skuId)函数,不在于使用jquery的each。优化要从设计上着手,参照你现有的业务。将“回传中泰运费”〔你的注释中是这么写的〕内部做优化。

你的这个函数应该是对一组数据作服务端回传调用。如果你是每一个.weight,一次调用的话,可以优化为“一次回传一组数据”。

web客户端展现优化,一般就两个方面:1、连接数〔重要〕;2、代码执行量

收获园豆:50
邢少 | 专家六级 |园豆:10926 | 2013-05-20 09:45

function cnthtypechange(id) {
//alert(weightTypeBox.select().val());
div_show(); //显示屏蔽层
var rankId = $("#<%=hfRankId.ClientID%>").val(); //
var weight = $(".weight[itemid=" + id + "]");
var volumes = $(".volumes[itemid=" + id + "]");
var goodqty = $(".auditqty[itemid=" + id + "]").val();
//衣服裤子、其他
var itemFreightTypeId = $(".ddlTypeTd[itemid=" + id + "]").children("select").select().val();
//海运、陆运
var transportTypeId = $("#ddlTransnationalFreightTypes").select().val();
var brandItem = $(".cbBrank[itemid=" + id + "]").children("input[type=checkbox]");
var isBrandItem = document.getElementById(brandItem.attr("id")).checked;
var calculateTypeSel = $(".freightMode[itemid=" + id + "]").children("select");
var calculateType = calculateTypeSel.val();
var sumChangeWeight = calculateWeightSum();

var sumChangeVolumn = calculateVolumnSum();

// $.post("/Ajaxs/AjaxPage.aspx", { ajaxType: "goodTypeFreight", transportTypeId: transportTypeId, itemFreightTypeId: itemFreightTypeId, isBrandItem: isBrandItem, weight: (weight.val() * goodqty), volume: (volumes.val() * goodqty), sumChangeWeight: sumChangeWeight, modeId: calculateType }, function (result) {
//alert(result);
//var arr = result.split(",");
$.post("/Ajaxs/AjaxPage.aspx", { ajaxType: "goodTypeFreight", transportTypeId: transportTypeId, itemFreightTypeId: itemFreightTypeId, isBrandItem: isBrandItem, weight: (weight.val() * goodqty), volume: (volumes.val() * goodqty), sumChangeWeight: sumChangeWeight, sumChangeVolumn: sumChangeVolumn, modeId: calculateType, rankId: rankId }, function (result) {
if (result.length > 0) {
var cnthAm = $(".cnthAmount[itemid=" + id + "]");
cnthAm.val(Number(result).toFixed(2));
var sum = 0;
$(".weight").each(function () {

if ($(this).val().length != 0) {
var itid = $(this).attr("itemid");
var qty = $(".auditqty[itemid=" + itid + "]").val();
sum += parseFloat($(this).val() * qty);
}
});
$("#sumWeight").text(sum.toFixed(3));
}
$(".sumGoodWeight").each(function () {
var id = $(this).attr("itemid");
$(this).text(Number(($(".auditqty[itemid=" + id + "]").val()) * ($(".weight[itemid=" + id + "]").val())).toFixed(3));
$(".sumGoodVolumn[itemid=" + id + "]").text((($(".auditqty[itemid=" + id + "]").val()) * ($(".volumes[itemid=" + id + "]").val())).toFixed(3));
});

pageSum();
div_close();
});
}

这是cnthtypechange(skuId)函数的实现。

在使用Ajax的函数里面已经有了一系列的优化,包括缓存!

比如说我现在又40个商品,之前是要去查40次数据库里面的阶梯价。但是缓存之后,只会查询一次。

其他39次都会从缓存里面读取!就我觉得Ajax已经不能再优化。

或者,在调用Ajax查询数据库的时候我能否把阶梯价的重量查询出来。

与穿过去的总重量进行比较,达到一定的阶梯价后才调用?

茶饭不思 | 园豆:123 (初学一级) | 2013-05-20 14:55

你所说的我那里是每一个weight调用一次的,也就是当修改一个商品的重量之后。每个商品都会调用一次,因为总重量是会影响到每个商品的运费的,如果优化的该如何优化?

茶饭不思 | 园豆:123 (初学一级) | 2013-05-20 14:57

@茶饭不思: 

看你贴出的代码.明显在遍历的时候,每发现有一个.weight,你就ajax post一次,如果有20个商品,是不是会请求服务器20次?这个是慢的一个很重要的原因。web优化 第一项:连接数

优化要结合你的实际应用场景,你post一次可能是更新后台的一项数据,你应该将你遍历的.weight的数据一起发送到的后台,而不是分多次.〔each〕,也就是说,each,把数据封装,调用一次post,这个是原则。剩下的就是就是结合业务,怎么实际重构一下。

邢少 | 园豆:10926 (专家六级) | 2013-05-20 15:11

@邢少: 恩,我也准备这样优化!多谢了,朋友。

茶饭不思 | 园豆:123 (初学一级) | 2013-05-20 15:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册