收藏
回答

小程序疯狂点击造成的bug

问题模块 框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
API和组件 小程序 Bug this.setData({}) 客户端 6.6.7 2.2.5

写了一个购物车。当用户疯狂点击添加商品时,可能会造成下面的这种情况


烦请大佬给点意见

最后一次编辑于  2018-11-09
回答关注问题邀请回答
收藏

1 个回答

  • 拾忆
    拾忆
    2018-11-09

    没有发代码也看不出什么问题来~

    猜测可能是频繁的操作setData导致~

    2018-11-09
    赞同
    回复 1
    • 你吼那么大声干什么
      你吼那么大声干什么
      2018-11-09

      addBuyNum(e, that) {

          let nowNum = e.currentTarget.dataset.addnum;//当前选中商品的数量

          let goodsId = e.currentTarget.dataset.goodsid;//商品id

          let currentGoodsList = that.data.checkGoodsType;//商品列表

          let iconType = e.currentTarget.dataset.type;//是增加商品还是减少商品

          let stock = e.currentTarget.dataset.stock;//获取到商品列表时得知的商品库存

          let index = e.currentTarget.dataset.index;

          let item = e.currentTarget.dataset.item;

          let shoppingCart = that.data.shoppingCart;//购物车中的商品

          let goodsInfo = that.data.goodsInfo;//总商品

          let ShoppingCartTotalPrice = 0;//购物车总价

          if (iconType == 'add') {

              if (nowNum >= stock) {//不请求数据先判断和之前的库存的差值

                  that.hud.showWarn("该商品库存不足", 2000);

                  item.buyNum = nowNum;

                  currentGoodsList.goodslist[index] = item;

                  that.setData({

                      checkGoodsType: currentGoodsList

                  });

              }else{

                  item.buyNum = nowNum+1;//库存够

                  currentGoodsList.goodslist[index] = item;

                  //保存选择状态

                  for (var goodsInfoItem in goodsInfo) {

                      if (goodsInfo[goodsInfoItem].id == currentGoodsList.id) {

                          goodsInfo[goodsInfoItem] = currentGoodsList;

                  }

              }

              that.setData({

                  checkGoodsType: currentGoodsList,

                  goodsInfo: goodsInfo

              });

      //加入购物车

          if (shoppingCart.length == 0) {

              item.totalPrice = (item.buyNum * item.saleprice);

              item.totalPrice = Common.numberToFixed(item.totalPrice);

              that.setData({    

                  shoppingCart: new Array(item),

                  ShoppingCartTotalPrice: item.totalPrice

              });

          } else {

                  if (shoppingCart.map(goodsInCart => goodsInCart.id).indexOf(goodsId) == -1) {

                      item.totalPrice = (item.buyNum * item.saleprice);

                      item.totalPrice = Common.numberToFixed(item.totalPrice);

                      shoppingCart = shoppingCart.concat(item);

                  } else {

                      shoppingCart.map((goodsInCart) => {

                          if (goodsId == goodsInCart.id) {

                                  goodsInCart.buyNum = goodsInCart.buyNum + 1;

                                  goodsInCart.totalPrice = (goodsInCart.buyNum * goodsInCart.saleprice);

                                  goodsInCart.totalPrice = Common.numberToFixed(goodsInCart.totalPrice);

                          }

                  });

                  shoppingCart = shoppingCart.concat([]);

              }

              ShoppingCartTotalPrice = +that.data.ShoppingCartTotalPrice + Number(item.saleprice);

              ShoppingCartTotalPrice = Common.numberToFixed(ShoppingCartTotalPrice);

              that.setData({

                  shoppingCart: shoppingCart,

                  ShoppingCartTotalPrice: ShoppingCartTotalPrice

              })

          }

              }

      } else {

      //减去商品数量

          item.buyNum = nowNum - 1;

          if (item.buyNum <= 0) { item.buyNum = 0 }

              currentGoodsList.goodslist[index] = item;

              for (var goodsInfoItem in goodsInfo) {

                  if (goodsInfo[goodsInfoItem].id == currentGoodsList.id) {

                      goodsInfo[goodsInfoItem] = currentGoodsList;

                  }

               }

             shoppingCart.map((goodsInCart) => {

              if (goodsId == goodsInCart.id) {

                  goodsInCart.buyNum = goodsInCart.buyNum - 1;

                  if (goodsInCart.buyNum == 0) {

                  var index = shoppingCart.indexOf(goodsInCart);

                          shoppingCart.splice(index, 1);

                  }

                  goodsInCart.totalPrice = (goodsInCart.buyNum * goodsInCart.saleprice);

                  goodsInCart.totalPrice = Common.numberToFixed(goodsInCart.totalPrice);

                  return;

              }

              });

              shoppingCart = shoppingCart.concat([]);

              ShoppingCartTotalPrice = +that.data.ShoppingCartTotalPrice - Number(item.saleprice);

              ShoppingCartTotalPrice = Common.numberToFixed(ShoppingCartTotalPrice);

              that.setData({

                  checkGoodsType: currentGoodsList,

                  goodsInfo: goodsInfo,

                  shoppingCart: shoppingCart,

                  ShoppingCartTotalPrice: ShoppingCartTotalPrice

               })

      }

      },


      2018-11-09
      回复