<small id='TcZJL'></small><noframes id='TcZJL'>

    <i id='TcZJL'><tr id='TcZJL'><dt id='TcZJL'><q id='TcZJL'><span id='TcZJL'><b id='TcZJL'><form id='TcZJL'><ins id='TcZJL'></ins><ul id='TcZJL'></ul><sub id='TcZJL'></sub></form><legend id='TcZJL'></legend><bdo id='TcZJL'><pre id='TcZJL'><center id='TcZJL'></center></pre></bdo></b><th id='TcZJL'></th></span></q></dt></tr></i><div id='TcZJL'><tfoot id='TcZJL'></tfoot><dl id='TcZJL'><fieldset id='TcZJL'></fieldset></dl></div>

  • <tfoot id='TcZJL'></tfoot>
  • <legend id='TcZJL'><style id='TcZJL'><dir id='TcZJL'><q id='TcZJL'></q></dir></style></legend>
      <bdo id='TcZJL'></bdo><ul id='TcZJL'></ul>

        javascript中switch case跳转到错误大小写(如何正确使用break命令)

        Switch case jumps to wrong case in javascript (how to properly use the break command)(javascript中switch case跳转到错误大小写(如何正确使用break命令))
          <bdo id='sKoo7'></bdo><ul id='sKoo7'></ul>
            1. <small id='sKoo7'></small><noframes id='sKoo7'>

                <tfoot id='sKoo7'></tfoot>

              • <i id='sKoo7'><tr id='sKoo7'><dt id='sKoo7'><q id='sKoo7'><span id='sKoo7'><b id='sKoo7'><form id='sKoo7'><ins id='sKoo7'></ins><ul id='sKoo7'></ul><sub id='sKoo7'></sub></form><legend id='sKoo7'></legend><bdo id='sKoo7'><pre id='sKoo7'><center id='sKoo7'></center></pre></bdo></b><th id='sKoo7'></th></span></q></dt></tr></i><div id='sKoo7'><tfoot id='sKoo7'></tfoot><dl id='sKoo7'><fieldset id='sKoo7'></fieldset></dl></div>

                  <tbody id='sKoo7'></tbody>

                  <legend id='sKoo7'><style id='sKoo7'><dir id='sKoo7'><q id='sKoo7'></q></dir></style></legend>
                  本文介绍了javascript中switch case跳转到错误大小写(如何正确使用break命令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的代码不是很长,所以我把它全部粘贴在这里.

                  My code is not so long so I am pasting all of it here.

                  代码不完整,但是当我运行它时,它首先跳转到它应该执行的 case start",然后跳转到 case end".我可以看到它,因为它会打印两个块的控制台日志文本.为什么要跳到结束"案例?

                  The code is not complete but when I run it it first jumps to case "start" which it is supposed to, and then jumps to case "end". I can see it because it prints both blocks' console log texts. Why is it jumping to the "end" case?

                  <html>
                      <body>
                          <script>
                              function stepStream(stream,step){
                                  switch (stream[step]){
                                      case "start":
                                          console.log("Started reading stream...");
                                      case "end":
                                          var success = "Finished reading dataStream.";
                                          console.log(success);
                                          return success;
                                      default:
                                          throw "Data stream format is bad";                  
                                      case "gesture":
                                          console.log("Running case gesture! But why?");
                                          step+=1;
                                          stepStream(stream,step);
                                      case "say":
                                          step+=1;
                                          stepStream(stream,step);
                                      case "sleep":
                                          step+=1;
                                          stepStream(stream,step);
                                  }
                  
                              }
                  
                              var sentence1 = "Where are my bananas? I thought you put them in my bag?";
                              var sentence2 = "This is a rather irritating situattion.";  
                              var dataStream = ["start","gesture","banzai","sleep",1.0,"say",sentence1,
                                                  "say",sentence2,"gesture","kubikasige","end"];
                              stepStream(dataStream,0);//Second parameter sets where to start reading the dataStream.
                  
                  
                          </script>
                      </body>
                  </html>
                  

                  推荐答案

                  问题是您的 case 代码后面缺少 break 关键字.没有中断,后续的块将被执行,这就是为什么 endstart 代码之后执行.您可以在此 W3Schools 链接上阅读更多相关信息.

                  The problem is that you are missing the break keyword after your case code. Without the break, subsequent blocks will be executed, that is why end is executed after start code. You can read more about this on this W3Schools link.

                  另外,来自 JS 参考:

                  与每个 case 标签关联的可选 break 语句确保一旦匹配的语句出现,程序就会跳出 switch在 switch 之后的语句处执行并继续执行.如果省略break,程序在下一个继续执行switch 语句中的语句.

                  The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

                  所以你的代码应该是这样的:

                  So your code should look like:

                  function stepStream(stream,step){
                                  switch (stream[step]){
                                      case "start":
                                          console.log("Started reading stream...");
                                          break;
                                      case "end":
                                          var success = "Finished reading dataStream.";
                                          console.log(success);
                                          return success;
                                      default:
                                          throw "Data stream format is bad";                  
                                      case "gesture":
                                          //commUGesture(stream[i+1]);
                                          //createLogLine("robot:CommU","event:gesture:"+stream[i+1]);
                                          console.log("Running case gesture! But why?");
                                          step+=1;
                                          stepStream(stream,step);
                                          break;
                                      case "say":
                                          step+=1;
                                          stepStream(stream,step);
                                          break;
                                      case "sleep":
                                          step+=1;
                                          stepStream(stream,step);
                                          break;
                                  }
                  

                  您的结束"案例最后有一个返回,因此代码不会落入其他案例.理想情况下,每个结尾都应该有一个 break.

                  Your "end" case has a return at the end, hence the code doesn't fall through to the other cases. Ideally, there should be a break at the end of each.

                  这篇关于javascript中switch case跳转到错误大小写(如何正确使用break命令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                      • <bdo id='7N7Fc'></bdo><ul id='7N7Fc'></ul>

                            <tbody id='7N7Fc'></tbody>

                          <small id='7N7Fc'></small><noframes id='7N7Fc'>

                          <legend id='7N7Fc'><style id='7N7Fc'><dir id='7N7Fc'><q id='7N7Fc'></q></dir></style></legend>
                          1. <tfoot id='7N7Fc'></tfoot>

                            <i id='7N7Fc'><tr id='7N7Fc'><dt id='7N7Fc'><q id='7N7Fc'><span id='7N7Fc'><b id='7N7Fc'><form id='7N7Fc'><ins id='7N7Fc'></ins><ul id='7N7Fc'></ul><sub id='7N7Fc'></sub></form><legend id='7N7Fc'></legend><bdo id='7N7Fc'><pre id='7N7Fc'><center id='7N7Fc'></center></pre></bdo></b><th id='7N7Fc'></th></span></q></dt></tr></i><div id='7N7Fc'><tfoot id='7N7Fc'></tfoot><dl id='7N7Fc'><fieldset id='7N7Fc'></fieldset></dl></div>