简体   繁体   English

如何使用开关案例范围的JavaScript

[英]how use switch case range JavaScript

I'm trying to using siwtch case JavaScript like this 我正在尝试像这样使用siwtch case JavaScript

 <p id="demo"></p>

  <script>
  var n=new Date();
  var jam=n.getHours();
  switch (jam) {
  case (jam>='17'):
    time = "sleep";
    break;
   case (jam<'17'):
    time = "work";
    break;     
   }
  document.getElementById("demo").innerHTML = "This Time for " + time;
  </script>

but this didn't appear anything 但这什么也没出现

You are using switch()case in incorrect way. 您以错误的方式使用了switch()case Because switch is used when you are trying to check states, but not ranges. 因为在尝试检查状态而不是范围时使用了switch If you want to check ranges, you'd rather use if()else state like this: 如果要检查范围,则宁愿使用if()else状态,如下所示:

<p id="demo"></p>

  <script>
  var n=new Date();
  var jam=n.getHours();

  if ( parseInt(jam) >= 17){
    time = "sleep";
  } else if ( parseInt(jam) <17)
    time = "work";
  } else {
    time = " play :) "
  }

  document.getElementById("demo").innerHTML = "This Time for " + time;
  </script>

You need to modify the switch case if you are trying to check range using switch , else you have to go for the traditional if else condition. 如果尝试使用switch来检查范围,则需要修改switch的大小写,否则必须采用传统的if else条件。 I have modified the code as per your requirement. 我已根据您的要求修改了代码。 Please check this one. 请检查这个。

  <html> <body> <p id="demo"></p> <script> var n=new Date(); var jam=n.getHours(); var time = ""; switch (true) { case (jam>=17): time = "sleep"; break; case (jam<17): time = "work"; break; } document.getElementById("demo").innerHTML = "This Time for " + time; </script> </body> </html> 
Using if else. 否则使用。

  <html> <body> <p id="demo"></p> <script> var n=new Date(); var jam=n.getHours(); var time = ""; if (jam>=17){ time = "sleep"; } else if (jam<17){ time = "work"; } document.getElementById("demo").innerHTML = "This Time for " + time; </script> </body> </html> 

You may use a case and use a condition in the case statement and true in the select part. 您可以在case语句中使用case并使用条件,在select部分中使用true

The use a fall trough style to check smalles value first and the bigger values. 使用下降槽样式先检查较小值和较大值。 The check must use numbers, not strings as in the example. 支票必须使用数字,而不是示例中的字符串。

at least you coluld use a default branch and assign somthing default like, which works when all other condition are not met. 至少您可以使用默认分支并分配类似默认值的东西,当不满足所有其他条件时,它可以工作。

 var n = new Date(), jam = n.getHours(), time = ''; switch (true) { case (jam < 9): // this leads to if (true === (jam < 9)) { ... time = "coffee"; break; case (jam < 17): time = "work"; break; case (jam < 19): time = "relaxing"; break; default: time = "sleep"; } console.log("This Time for " + time); console.log(jam); 

I have fixed your code, here is the js fiddle 我已经修复了您的代码,这是js小提琴

https://jsfiddle.net/qkpmkzoh/ https://jsfiddle.net/qkpmkzoh/

And the fixed code is this. 固定的代码是这个。

  var n=new Date();
  var jam=n.getHours();
  var time = "";
  console.log(jam);
  switch (true) {
  case jam>=17:
    time = "sleep";
    break;
   case jam<17:
    time = "work";
    break;     
   }
  document.getElementById("demo").innerHTML = "This Time for " + time

However I would recommend you to use if else, as it makes the code more readable 但是,我建议您使用其他方式,因为这会使代码更易读

  var n=new Date();
  var jam=n.getHours();
  var time = "";
  console.log(jam);
  if (jam>=17){
   time = "sleep";
  }
    else if(jam<17) {
    time = "work";
  }  

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM