在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ js函數(shù)返回值賦予變量,函數(shù)還沒執(zhí)行完,變量已經(jīng)賦值

js函數(shù)返回值賦予變量,函數(shù)還沒執(zhí)行完,變量已經(jīng)賦值

var arrPicture11 = util.redraw(that.data.src, that.data.screenWidth, that.data.screenHeight, that.data.rectanglesBeginx, that.data.rectanglesBeginy, that.data.rectanglesWidth, that.data.rectanglesHeight)
    console.log(arrPicture11)

已下是封裝的函數(shù)

function redraw(imgSrc, screenWidth, screenHeight,theBeginx,theBeginy,theWidth,theHeight){
  wx.getImageInfo({
    src: imgSrc,
    success: function (res) {
      // 判斷圖的長(zhǎng)
      if (res.height <= 1096) {
        var h = screenWidth * res.height / res.width;
        var beginy = (screenHeight - h - 119) / 2;
        var w = screenWidth;
        var beginx = 0;
        if (beginy < 0) {
          beginy = 0;
        }
      } else {
        var w = (screenHeight-119) * res.width / res.height;
        var beginx = (screenWidth - w) / 2;
        var h = (screenHeight-119);
        var beginy = 0;
        if (beginx < 0) {
          beginx = 0;
        }
      }

      //用canvas繪制圖片
      const ctx = wx.createCanvasContext('myCanvas');
      ctx.stroke();
      ctx.drawImage(imgSrc, beginx, beginy, w, h);
      ctx.globalCompositeOperation = "destination-over";
      ctx.setLineWidth(4);
      ctx.setStrokeStyle('red');
      ctx.strokeRect(theBeginx+2, theBeginy+2, theWidth-5, theHeight-5);
      ctx.draw();
      arr = [theBeginx + 2 - beginx, theBeginy + 2 - beginy, theBeginx + theWidth - 3 - beginx, theBeginy + theHeight - 3 - beginy];
      console.log(arr);
      
    }
  })
  setTimeout(function () {
    console.log(arr);
    return arr;
  }, 100);
  
}

封裝的函數(shù)中出現(xiàn)了異步問題,所以加了定時(shí)器,已下是打印數(shù)據(jù)

clipboard.png
第一行是第一串代碼打印的值undefined
第二行和第三行是函數(shù)中的打印值
現(xiàn)在就是想知道怎么解決那個(gè)賦值是undefined的問題

回答
編輯回答
青瓷

你都說了是異步問題了,直接賦值肯定會(huì)有問題啊,所有依賴于請(qǐng)求的操作都要放在異步回調(diào)里。

2018年1月28日 18:05
編輯回答
柚稚

使用async await

2017年7月13日 06:41
編輯回答
舊顏

在外部聲明即可,將賦值方法部分放在請(qǐng)求的成功回調(diào)里就可以了。

2018年4月7日 16:15