简体   繁体   English

为什么每次运行函数时if语句都与else交替出现

[英]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. 所以在这里,我通过按钮创建了一个函数,使用ajax上传图像以在画布上显示,但是为了指定宽度和高度,我使用了if语句。 I specify if width greater than height the width = 300 and height = 200 , if width less than height width = 200 and height = 300 . 我指定width是否大于height, width = 300height = 200 ,如果width小于height width = 200height = 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. 但是我不知道如果我在刷新页面后第一次单击按钮,if语句不正确,或者有时是正确的。 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 抱歉,这是我成功的全部ajax代码

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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