使用此代码的正确方法是什么?

时间:2016-08-03 08:55:03

标签: javascript jquery

此代码适用于即将推出的倒计时插件页面。我怎样才能以简短的方式或正确的方式编写这段代码?

$(document).ready(function() {
    else if(month == 2){
        var currentMo = 2564;
    }else if(month == 3){
        var currentMo = 452136;
    }else if(month == 4){
        var currentMo = 86985;
    }else if(month == 5){
        var currentMo = 1236;
    }else if(month == 6){
        var currentMo = 12365;
    }else if(month == 7){
        var currentMo = 12546;
    }
});

2 个答案:

答案 0 :(得分:3)

您可以在此处使用switch声明:

var currentMo;
switch (month) {
  case 1:
    currentMo = 1337;
    break;
  case 2:
    currentMo = 2564;
    break;
  case 3:
    currentMo = 452136;
    break;
  // ...
}

甚至是具有预定义值的数组:

var months = [1337, 2564, 452136];
var currentMo = months[month - 1];

答案 1 :(得分:0)

简而言之,

$(function() {
    var currentMo;
    if(month == 1)
       currentMo  = 2564;
    else if(month == 2)
        currentMo  = 2564;
    else if(month == 3)
        currentMo = 452136;
    else if(month == 4)
        currentMo = 86985;
    else if(month == 5)
        currentMo = 1236;
    else if(month == 6)
        currentMo = 12365;
    else if(month == 7)
        currentMo = 12546;    
});