Javascript: if (x <如果(x<= Y)不工作,则Y)工作

Javascript: if (x < y) works, if(x<=y) does not work.

本文关键字:工作 if Javascript 如果      更新时间:2023-09-26

if (x <如果(x><=y)在Javascript中不起作用。在前一种情况下,if语句被求值,在后一种情况下,if语句被忽略。

代码如下:

var totalFrames = 12;

showvalue函数从HTML中接收值:

<input type="range" min="2" max="36" step="2" value="2" name="totalFrames" 
      onChange="showValue(this.value);" width: 80%" value="2"/>
<p>Total Frames: <span id="totalFrames">2</span> 

function showValue(newValue) {
         document.getElementById("totalFrames").innerHTML = parseFloat(newValue);
         totalFrames = parseInt(newValue);
         var winkel = 360.0/totalFrames;
         document.getElementById("angle").innerHTML = winkel.toFixed(1);
         var maxFrame = document.getElementsByName("actFrame")[0];
         maxFrame.max = parseInt(newValue);
}
function motorCommunication() { 
      actFrame = document.getElementById("actFrame").value;
      actualFrame = parseInt(document.getElementById("actFrame").value);
      document.getElementById("range").innerHTML = actFrame; 

/*这行有效——if语句不会被忽略*/

if ( Number(actualFrame) < Number(totalFrames) ) { 

/*这行不行——if语句被忽略*/

if ( Number(actualFrame) <= Number(totalFrames) ) {
     var url = "/cgi-bin/bn_events.py?actualFrame=" + escape(actualFrame) 
         + "&totalFrames=" + escape(totalFrames) 
         + "&seconds=" + escape(seconds);
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
     actualFrame++;
  }
  else {
       document.getElementById("returnedStatus").value = "DONE.";
  }
}
function updatePage() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      /* Get the response from the server */
      var motorResponse = request.responseText;
      motorData = new Array();
      //motorData = request.responseText;
      motorData = motorResponse.substring(0,motorResponse.length-1).split(",");
      /* Update the HTML web form */
      document.getElementById("returnedFrame").value = motorData[0];
      document.getElementById("returnedTotalFrames").value = motorData[1]
      document.getElementById("returnedStatus").value = motorData[2];
      document.getElementById("actFrame").value = parseInt(actualFrame);
      document.getElementById("actualStatus").value = parseInt(actualFrame);     
      setTimeout(motorCommunication(),300);
      } else {
      alert("Error! Request status is " + request.status);
      }
    }
}

我发现,如果我使用

if ( Number(actualFrame) <= 12 ) {

它工作得很好....

现在无助……感谢任何评论。

修复了…与str到Int或类似的无关…if语句的位置是错误的。所以有些变量搞错了。

function motorCommunication() {
         /* NEEDS TO BE HERE... */
         if ( actualFrame <= lastFrame * 1.0 ) {
         actFrame = document.getElementById("actFrame").value;
         actualFrame = parseInt(document.getElementById("actFrame").value);
         document.getElementById("range").innerHTML = actFrame * 1.0;  
         //lastFrame = parseInt(document.getElementsByName("totalFrames")[0].value); 
         document.getElementById("returnedTotalFrames").value = lastFrame * 1.0;
         //WRONG HERE: if ( actualFrame <= lastFrame * 1.0 ) {
            var url = "/cgi-bin/bn_events.py?actualFrame=" + escape(actualFrame) 
            + "&totalFrames=" + escape(lastFrame) 
            + "&seconds=" + escape(seconds);
            //var url = "/cgi-bin/bn_events.py?actualFrame=" + escape(actFrame);
            request.open("GET", url, true);
            request.onreadystatechange = updatePage;
            request.send(null);
            actualFrame++;
            } else {
            document.getElementById("returnedStatus").value = "DONE.";
         }
}

现在效果更好了。独立于浏览器…

谢谢你的帮助。彼得。