Javascript:if(x< y)有效,if(x< = y)不起作用。

时间:2013-04-05 12:27:42

标签: javascript operators equals

if(x&lt; y)起作用,if(x <= y)在Javascript中不起作用。在前一种情况下,if语句被评估,在后一种情况下,if-staement被忽略。

以下是代码:

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 ) {

它完美无缺......

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

1 个答案:

答案 0 :(得分:0)

修好了...与str无关或类似的...... 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.";
         }
}

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

感谢您的帮助。彼得