我有一个函数,可以根据页面上的输入来计算总帧数。我已经快完成了,但是问题是,如果我插入783这样的整数,那么我不知道如何使它计算得出并知道它是一个单帧。我希望它每次在该特定输入字段上输入用户输入时就计算出帧的总数。

这是输入及其工作方式的示例。

1-10 will render all frames from 1 to 10, and the total amount of frames is 10.
1-10,2 will render every second frame in the specified range of 1 to 10, and the total amount of frames is 5.
1-4;10-20,2 will render frames 1, 2, 3, 4, 10, 12, 14, 16, 18, 20, and the total amount of frames is 10.
1-4;10-20,2;83 will render frames 1, 2, 3, 4, 10, 12, 14, 16, 18, 20, 83, and the total amount of frames is 11.
7 will render frame number 7 and the total amount of frames will be 1.
83 will render frame number 83 and the total amount of frames will be 1.

So `;` is like a separator
x - y is from frame x to frame y
x - y , z is every z frame between frame x and frame y




            var totFrames = 0;

            function check() {
              var input = document.getElementById("id_desiredFrames").value;
              var chunk = input.split(";");
              chunk.forEach(processChunk);

              if (totFrames && totFrames !== 0) {
                document.getElementById("TotalFrames").value = totFrames;
                totFrames = 0;
              } else {
                document.getElementById("TotalFrames").setAttribute("type", "hidden");
              }
            }

            function processChunk(chunk) {
              var stepInfo = chunk.split(",");
              var step = 1;
              if (stepInfo.length > 1 && stepInfo[1] > 1) step = stepInfo[1];

              var range = stepInfo[0].split("-");
              var frame = Math.round((range[1] - range[0] + 1) / step);
              if (frame && frame > 0) {
                totFrames += frame;
              } else {
                totFrames = 0;
              }
            }

<input type="text" name="desiredFrames" maxlength="15" placeholder="1-500" onchange="check()" class="Blenderfield" required="" id="id_desiredFrames">
<input type="hidden" id="TotalFrames" name="TotalFrames" value="">

最佳答案

您没有想到只能像这样的例子提供一个数字:1-4; 10-20,2; 83 83没有对。此外,当frame


            var totFrames = 0;

            function check() {
              var input = document.getElementById("id_desiredFrames").value;
              var chunk = input.split(";");
              chunk.forEach(processChunk);
              // Check totFrames
              console.log(totFrames);

              if (totFrames && totFrames !== 0) {
                document.getElementById("TotalFrames").value = totFrames;
                // Why do you reset totFrames?
                totFrames = 0;
              }
            }

            function processChunk(chunk) {
              var stepInfo = chunk.split(",");
              var step = 1;
              if (stepInfo.length > 1 && stepInfo[1] > 1) step = stepInfo[1];

              var range = stepInfo[0].split("-");
              var frame = 0;
              // Check what you're getting here
              if(range.length == 1) {
                  // Only one frame here
                  frame = 1;
              } else {
                  // Maybe you need to check if range[1] is greater than range[0]
                  // Use parseInt to convert values to integer
                  frame = parseInt(range[1]) - parseInt(range[0]);
              }
              if(frame > 0) {
                totFrames += frame;
              // This else resets the counter, you don't need it
              // } else {
              //  totFrames = 0;
              }
            }

<input type="text" name="desiredFrames" maxlength="15" placeholder="1-500" onchange="check()" class="Blenderfield" required="" id="id_desiredFrames">
<input type="hidden" id="TotalFrames" name="TotalFrames" value="">

10-07 21:11