-1

if (x < y) 有效, if(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 ) {

它完美地工作....

现在无能为力...感谢任何评论。

4

1 回答 1

0

搞定了...与 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.";
         }
}

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

感谢你的帮助。彼得

于 2013-04-05T20:48:52.663 回答