简体   繁体   中英

why my if statement alternates with else every time i run the function

So here i made a function from a button to upload images using ajax to show on the canvas but to specify the width and height I use the if statement. I specify if width greater than height the width = 300 and height = 200 , if width less than height width = 200 and height = 300 .

But i dont know how the if i click the button for first time after refresh my page the if statement is not correct or else sometime it is. And the next following click is alternately. Here my code.

      var image2 = new Image();   
      image2.src = '../img/template/'+e; 
      tmpWidht = parseInt(image2.width);
      tmpHeight = parseInt(image2.height);

      if( tmpWidht > tmpHeight ){
        var imgWidth = 300;
        var imgHeight = 200;
      }else{
        var imgWidth = 200;
        var imgHeight = 300;
      }

ah sorry this is the all of my success ajax code

var id = e+varImg;
      var image2 = new Image();   
      image2.src = '../img/template/'+e; 
      tmpWidht = parseInt(image2.width);
      tmpHeight = parseInt(image2.height);

      if( tmpWidht > tmpHeight ){
        var imgWidth = 300;
        var imgHeight = 200;
      }else{
        var imgWidth = 200;
        var imgHeight = 300;
      }

      image2.onload = function(){
        var img2 = new Konva.Image({
        x:50,
        y:50,
        width: imgWidth,
        height: imgHeight,
        image: image2,
        id: id,
        draggable: true
        });

      layer.add(img2);
      layer.draw();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM