使用JavaScript应用DRY原则

时间:2016-02-06 16:25:48

标签: javascript dry design-principles

我目前无法在此方案中尝试应用DRY原则。这是代码。如您所见,我正在尝试在不同的日子运行包含不同变量的脚本。我的意思是,我可以在周五之前发出一堆其他if语句,但这似乎不是正确的做事方式。有什么指针吗?

由于

<script>
    function timer(){
        window.alert("The timer will start in 1 minute. Get ready!");
        var dayVar = new Date().getDay();
           if(dayVar == 1){
             var monPhp = "<?php echo $homeMon2 ?>";
             var min = 60 * monPhp;
               setInterval(function(){
                 var a = new Date();
                 document.getElementById("result").innerHTML = "Minutes : " + min ;
                 min--;
                if(min == -1){
                  window.alert("Times up!");
                  min = 60 * monPhp;
                }           
               },60000); 
           } else if (dayVar == 2){
               var tuePhp = "<?php echo $homeTue2 ?>";
               var min = 60 * tuePhp;
               setInterval(function(){
                  var a = new Date();
                  document.getElementById("result").innerHTML = "Minutes : " + min ;
                  min--;
                  if(min == -1){
                      window.alert("Times up!");
                      min = 60 * tuePhp;
                  }
               },60000); 
            }
      }
</script>

2 个答案:

答案 0 :(得分:1)

在PHP中,创建一个数组

<?php

    $arr = Array($homeSun2, $homeMon2, $homeTue2, $homeWed2 ... etc);
?>

然后使用它

function timer(){
    var days   = <?php echo json_encode($arr); ?>;
    var dayVar = new Date().getDay();

    window.alert("The timer will start in 1 minute. Get ready!");

    var min  = 60 * days[dayVar];
    var temp = min;

    setInterval(function(){
        var a = new Date();
        document.getElementById("result").innerHTML = "Minutes : " + (temp--) ;
        if(temp == -1){
            window.alert("Times up!");
            temp = min;
        }           
    },60000);
}

答案 1 :(得分:0)

试试这个!

  function timer() {
        window.alert("The timer will start in 1 minute. Get ready!");
        var dayVar = new Date().getDay();
        var day = 0;
        switch (dayVar) {
            case 1 :
                day = 1; 
                break;
            case 2 :
                day = 2;
                break;
            case 3 :
                day = 3;
                break;
            case 4 :
                day = 4;
                break;
            case 5 :
                day = 5;
                break;
            case 6 :
                day = 6;
                break;
            case 7 :
                day = 7;
                break;
            default :
                break;
        }

        var min = 60 * day;
        setInterval(function () {
            var a = new Date();
            document.getElementById("result").innerHTML = "Minutes : " + min;
            min--;
            if (min == -1) {
                window.alert("Times up!");
                min = 60 * day;
            }
        }, 60000);

    }