如果循环停止而不工作

时间:2016-04-09 15:45:38

标签: javascript

if (skin_var == 1) {
        skin = "%kraken";

        skin_var = 2;
    }
    if (skin_var == 2) {
        // this won't activate
        skin = "%mercury";

        skin_var = 3;
    }
    if (skin_var == 3) {
        skin = "%shark";

        skin_var = 4;
    }
    if (skin_var == 4) {
        // this won't activate either
        skin = "%banana";

        skin_var = 5;
    }
    if (skin_var == 5) {
        skin = "%nuclear";

        skin_var = 6;
    }
    if (skin_var == 6) {
        skin = "%space_dog";

        skin_var = 7;
    }
    if (skin_var == 7) {
        skin = "%t_rex"

        skin_var = 8;
    }
    if (skin_var == 8) {
        // gets stuck right here
        skin = "%spy"

        skin_var = 1;
    }

这是我上面的代码。正如您在评论中所看到的那样,他们总是不会工作或者在某些时候陷入困境。有没有一种有效的方法来做到这一点或一种方法来解决它?

我只想永远将皮肤改为每秒,我使用了setInterval();但它似乎只是做了"%kraken"鲨鱼"%鲨鱼"到"%核"到"%t_rex"然后它在我添加"%spy"。

之前循环

4 个答案:

答案 0 :(得分:0)

// Instead of switch or if statement I made an array with all values
var values = ["%kraken", "%mercury", "%shark", "%banana", "%nuclear", "%space_dog", "%t_rex", "%spy"];

var i = 0;

// Foreach loop I set a different value to skin variable, with the i value. When i variable is great than values' length I set it to zero (interval is 1000 millisecond)
setInterval(function(){ 
    if (i > values.length -1) {
        i = 0;
    }
    skin = values[i];
    i++;
}, 1000);

答案 1 :(得分:0)

我试图保存已添加setInterval并使用运算符%的代码来使skin_val var的值始终保持在0到7的范围内

var skin_var = 0;
var skin;
function changeSkin(){
    if (skin_var == 0) {
    skin = "%kraken";
}
if (skin_var == 1) {
    // this won't activate
    skin = "%mercury";
}
if (skin_var == 2) {
    skin = "%shark";
}
if (skin_var == 3) {
    // this won't activate either
    skin = "%banana";
}
if (skin_var == 4) {
    skin = "%nuclear";
}
if (skin_var == 5) {
    skin = "%space_dog";
}
if (skin_var == 6) {
    skin = "%t_rex"
}
if (skin_var == 7) {
    // gets stuck right here
    skin = "%spy"
}
skin_var = (skin_var+1)%8
console.log(skin);
}
window.setInterval(changeSkin, 1000);

您可以在控制台日志中看到结果。

答案 2 :(得分:0)

此答案使用简单的递归setTimeout代替。

var skins = [
  '%kraken', '%mercury', '%shark', '%banana', '%nuclear', '%space_dog', '%t_rex', '%spy'
];

var skin;

function changeSkin(i) {
  skin = skins[i = i && i < skins.length ? i : 0];
  setTimeout(changeSkin, 1000, ++i);
}

changeSkin();

DEMO

答案 3 :(得分:-1)

在您的情况下,switchif更具可读性和效率。 这样,您只需查看skin_var值一次,而不是每次if

这就是你所要求的。运行时确保控制台已打开。

&#13;
&#13;
function changeSkin(){
  switch (skin_var){
    case 1: 
        skin = "%kraken";
        break;      
    case 2 : 
        skin = "%mercury";
        break;      
    case 3: 
        skin = "%shark";
        break;      
    case 4: 
        skin = "%banana";
        break;      
    case 5: 
        skin = "%nuclear";
        break;      
    case 6:
        skin = "%space_dog";
        break;      
    case 7:
        skin = "%t_rex"
        break;      
    case 8:
        skin = "%spy"
        skin_var = 0;
        break;
    default:
        skin_var = 0;
        break;
  }
  ++skin_var;
  console.log(skin);
}
var skin_var = 1;
setInterval(changeSkin, 1000)
&#13;
&#13;
&#13;