使用 JavaScript 应用 DRY 原则
Apply DRY principle with JavaScript
我目前无法在这种情况下尝试应用 DRY 原则。这是代码。如您所见,我正在尝试 运行 脚本在不同的日子包含不同的变量。我的意思是,我可以简单地在星期五之前发出一堆 else 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>
在您的 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);
}
试试这个!
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);
}
我目前无法在这种情况下尝试应用 DRY 原则。这是代码。如您所见,我正在尝试 运行 脚本在不同的日子包含不同的变量。我的意思是,我可以简单地在星期五之前发出一堆 else 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>
在您的 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);
}
试试这个!
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);
}