开发者

javascript - switch not working

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);    
}

how to do 开发者_运维知识库that switch is a way it works?


break statements for your switch, and proper spelling of "default" should help you on your way :)

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);    
}


Two Problems:

  • You are missing the break keyword after each case clause.
  • Your spelling for the default was wrong.

.

   switch (level) {
        case "addtoprio()":log = true; break;
        case "alternaTropas()":log = false; break;
        case "sendtroops()":log = false; break;
        default: log = false; break;
    }

More On Switch


You must add break instruction after each case, and use default not defalt.


Change it to this:

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);    
}


I had a similar situation where adding a break statement did not correct the problem. I finally corrected the problem by making sure I passed a string type into the switch statement. You can do this in your case by var newStr = level + "";. The + "" will ensure it's a string type. Then pass newStr into your switch statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜