Javascript: if (x <如果(x<= Y)不工作,则Y)工作
Javascript: if (x < y) works, if(x<=y) does not work.
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.";
}
}
现在效果更好了。独立于浏览器…
谢谢你的帮助。彼得。
相关文章:
- 正在尝试使用if和else添加类,但无法正常工作
- jquery onclick if..否则如果..否则无法正常工作
- if 子句在 JavaScript 中限定工作流
- 此 JavaScript 代码无法正常工作(if-else 语句)
- JqueryUI,多个ID和else if 语句在可丢弃,我如何让它们工作
- 用于检查文件扩展名不工作的Javascript If语句
- 当if条件被添加到循环中时,Javascript函数将停止工作
- 为什么更改范围获胜'不能在ng if内部工作,而是通过函数调用AngularJS
- else-if子句未按预期工作
- 为什么不'我的if/else语句不能正常工作
- 可以't使if-else-jquery语句工作
- for循环,if else不工作Javascript
- Check IF条件返回false,但IF中的语句仍然可以在JavaScript中工作
- Javascript/JQuery嵌套if语句工作不正常
- Javascript if/else工作不正常[Time Formatting AM/PM]
- 为什么我的 IF 无法正常工作
- if.else 在 IIFy 中工作,但不在事件侦听器中工作
- 带有 if-else 的功能无法正常工作
- KnockoutJS “if: ” 绑定无法正常工作
- if 和 else-if 语句拒绝在 jQuery 的 AJAX 函数中工作