简体   繁体   English

如何检查if语句的条件以查看其是否正常运行?

[英]How do I check my condition on an if statement to see if it is working correctly?

I'm trying to make a type of circular display which cycles through a series of values as well as moving text elements within an svg file. 我正在尝试制作一种圆形显示,该显示循环显示一系列值以及在svg文件中移动文本元素。 It uses the hammer.js library and uses the drag event. 它使用Hammer.js库并使用drag事件。 The values can go in either direction. 值可以沿任一方向进行。 I have it working to some degree. 我有一定程度的工作。 As the last value is shown from an array, it goes back to the beginning of the array to get the first values. 当从数组中显示最后一个值时,它返回到数组的开头以获取第一个值。 Or vice-versa. 或相反亦然。

var  keyArray = ["C","C#","Db","D","D#","Eb","E","F","F#","Gb","G","G#","Ab","A","A#","Bb","B"];

This is my array. 这是我的数组。 Here is how I wrap it past the end of the array and back to the beginning. 这是我将其包装在数组末尾并返回到开头的方式。

** As per the request of a few commenters and the suggested solution by Nina, I have modified the code below to reflect their suggestions. **根据一些评论者的要求和Nina提出的建议解决方案,我修改了以下代码以反映他们的建议。 I have also added a few more lines for clarity of what is happening overall.** 为了清楚说明整体情况,我还添加了几行内容。**

var delta = keyArray.length - 5; // can be constant, it is always positive  

for(i=0;i<5;i++){
  //5 svg text element containing 5 musical keys
  keys = document.getElementById("keys"+i);
  //ev.deltaX is the change received from the hammer pan event
  //text element moves relative to its original starting X
  keys.setAttribute("x",startingSpots[i]+ev.deltaX%150);
  currentX=keys.getAttribute("x");
  currentEdgeIndex=keyArray.indexOf(keys.textContent);    

  //This if is what appears to be failing.  
  if (keys.getAttribute("x")>=565){
    keys.setAttribute("x",currentX-150);
    keys.textContent = keyArray[(currentEdgeIndex + delta) % keyArray.length];
  }  
}

With the suggested changes, I removed the Number() calls as well as implementing the modulus for the wrapper. 通过建议的更改,我删除了Number()调用并实现了包装器的模数。 The behavior is still erratic. 该行为仍然不稳定。 On the example below, if you pan to the right, as the first text element reaches 565, it meets the condition for the if, is moved back to the left by 150. 在下面的示例中,如果向右平移,则第一个文本元素达到565时,它满足if条件,即向左移动150。

What it should do next is to change the textContent to the next appropriate value in the array. 接下来应该做的是将textContent更改为数组中的下一个适当的值。 However, this is where it becomes erratic, it is no longer past 565 so it does not meet the condition of the if statement, but the text changes at every increment of the pan event as if it were. 但是,在这里它变得不稳定,不再超过565,因此它不满足if语句的条件,但是文本在pan事件的每个增量处都将发生变化,就好像它是那样。

I am sure I am not seeing something simple that is causing the trouble but not sure what it is. 我确定我没有看到引起麻烦的简单原因,但不确定是什么原因。

The array does appear to be circling correctly, though I'm still not sure "How can I check to see if the if statement is being correctly evaluated and met?" 尽管我仍然不确定“如何检查if语句是否正确求值并得到满足?”,但数组似乎确实在正确地循环。

The project can be viewed here. 该项目可以在这里查看。 http://codepen.io/cmgdesignstudios/pen/zrmQaE?editors=1010 http://codepen.io/cmgdesignstudios/pen/zrmQaE?editors=1010

* Edit with solution * Nina suggested the problem lie in the handling of the touch event. *使用解决方案进行编辑*妮娜建议问题出在触摸事件的处理上。 After further investigation, I found she was correct. 经过进一步调查,我发现她是正确的。 I had originally been moving the object relative to its starting position and the deltaX from the touch event. 我最初是相对于对象的起始位置和触摸事件中的deltaX移动对象。 I then was trying to change the current position by simply moving it back to the left rather than adjusting the starting position. 然后,我试图通过简单地将其向左移动而不是调整起始位置来更改当前位置。

So I replaced the 所以我换了

keys.setAttribute("x",currentX-150);

with

startingSpots[i]-=150;

This kept the movement relative to the starting position and the deltaX of the touch event. 这使运动相对于触摸事件的开始位置和增量保持不变。

Please delete the Number(...) casting where it's not necessary. 请删除不必要的Number(...)强制类型转换。 ...length returns always number and the result of calculation is a number too. ...length总是返回数字,计算结果也是一个数字。

Your actual key feature is to move 5 entries down, and this can be achieved wit a simple addition and a modulo, to keep the value in a specific range, like 您的实际关键特征是向下移动5个条目,这可以通过简单的加法和取模来实现,以将值保持在特定范围内,例如

keys.textContent = keyArray[(keyArray.length + currentEdgeIndex - 5) % keyArray.length];

Further simplified can this calculation then lead to just add a positive number of 可以进一步简化此计算,然后得出正数即可

delta = keyArray.length - 5; // can be constant, it is always positive
keys.textContent = keyArray[(currentEdgeIndex + delta) % keyArray.length];

and make the modulo out of it. 并用它做模

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

相关问题 如何检查Node 6 REPL中的import语句是否正常工作? - How can I check if an import statement is working correctly in the Node 6 REPL? 我如何不断检查我的 if 语句是否返回 true 或 false - How do I constantly check if my if statement returns true or false 如何正确实现此 switch 语句? - How do I correctly implement this switch statement? 如何正确将此 if 语句放入 function 中? - How do I put this if statement into a function correctly? 如果满足条件,我该如何“重启” if语句? - How do I “restart” if statement if a condition is met? 如何检查我的单选按钮是否已选中并且循环正常工作? - How do I check if my radio button is selected and the loop is working? 如何让 WebStorm 正确查看包功能? - How do I get WebStorm to correctly see a packages functions? 如何检查条件是否为真并运行代码? 下面的代码不起作用 - How do I check if condition is true and run code? It's not working code below 我如何使用 lodash 检查集合中的每个项目,除了那些不符合我的条件的项目? - How do I use lodash to check every item in a collection except those that dont meet my condition? 当我尝试在我的代码中使用它来检查变量时,if 语句不起作用 - if statement not working when i try to use it in my code to check a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM