function FM_log(level, text) {
// caso não seja log total escolhe o que loga
var log = false;
switch (level) {
case "addtoprio()":log = true;
case "alternaTropas()":log = false;
case "sendtroops()":log = false;
defalt: log = false;
}
if ((logTotal == false) && (log == true))
GM_log(horaAtual() + " - "+level+", "+text);
else if (logTotal == true)
GM_log(horaAtual() + " - "+level+", "+text);
}
如何进行切换是一种有效的方式?
答案 0 :(得分:8)
break
语句,以及“默认”的正确拼写应该可以帮助您:)
function FM_log(level, text)
{
// caso não seja log total escolhe o que loga
var log = false;
switch (level)
{
case "addtoprio()":
log = true;
break;
case "alternaTropas()":
log = false;
break;
case "sendtroops()":
log = false;
break;
default:
log = false;
break;
}
if ((logTotal == false) && (log == true))
GM_log(horaAtual() + " - "+level+", "+text);
else if (logTotal == true)
GM_log(horaAtual() + " - "+level+", "+text);
}
答案 1 :(得分:4)
两个问题:
break
子句后,您缺少case
个关键字。default
的拼写错误。
switch (level) {
case "addtoprio()":log = true; break;
case "alternaTropas()":log = false; break;
case "sendtroops()":log = false; break;
default: log = false; break;
}
答案 2 :(得分:2)
你必须在每个案例后添加break指令,并使用default not defalt。
答案 3 :(得分:2)
将其更改为:
function FM_log(level, text) {
// caso não seja log total escolhe o que loga
var log = false;
switch (level) {
case "addtoprio()":log = true; break;
case "alternaTropas()":log = false; break;
case "sendtroops()":log = false; break;
defalt: log = false; break;
}
if ((logTotal == false) && (log == true))
GM_log(horaAtual() + " - "+level+", "+text);
else if (logTotal == true)
GM_log(horaAtual() + " - "+level+", "+text);
}
答案 4 :(得分:0)
我遇到类似的情况,添加break语句并没有解决问题。我终于纠正了这个问题,确保我将一个字符串类型传递给switch语句。您可以通过var newStr = level + "";
在您的案例中执行此操作。 + ""
将确保它是字符串类型。然后将newStr
传递给您的switch语句。