简体   繁体   English

循环Java代码[循环不会停止]

[英]Javascript for loop [loop doesn't stop]

I really try to find out why the for loop is looping again and again. 我真的试图找出为什么for循环一次又一次地循环。 My question is why is the first for looping again and again thought x is 1? 我的问题是为什么第一个循环一次又一次认为x为1?

The result shows random counts of progressbars with a random progresses (img element is the progress). 结果显示具有随机进度的进度条的随机计数(img元素为进度)。 But it should only show 1 because x is 1. Can somebody tell me whats the answer? 但是它应该只显示1,因为x是1。有人可以告诉我答案是什么吗?

function progress(){

    var min = 0;
    var max = 10;
    /*var x = Math.floor(Math.random() * (max - min)) + min;*/
    var x = 1;


    var main_div = document.createElement('div');
    main_div.className = "main_div";
    document.body.appendChild(main_div);

    for(var i = 0; i < x; i++){

        var einfuegen = document.createElement('div');
        einfuegen.className = 'statusbar';
        main_div.appendChild(einfuegen);    

        var einfuegen2 = document.createElement('img');
        einfuegen2.id = 'bild';
        einfuegen2.name = 'bild';
        einfuegen2.src = 'project_status.gif';  

        var zielort = document.getElementsByClassName('statusbar')[i];
        zielort.appendChild(einfuegen2);

        var min = 0;
        var max = 100;
        var x = Math.floor(Math.random() * (max - min)) + min;
        document.getElementsByTagName('img')[i].style.width = x+"%"; 


    }

}

You're changing x here: 您要在此处更改x

var x = Math.floor(Math.random() * (max - min)) + min;

So the loop will loop a random number of times between 0 and 100. 因此,循环将在0到100之间循环随机次数。

Use a different variable name for the value of your progress bar, and for that matter, the max and min values of the progress bar: 使用不同的变量名作为进度条的值,并为此使用进度条的最大值和最小值:

var value = Math.floor(Math.random() * (maxValue - minValue)) + minValue;
document.getElementsByTagName('img')[i].style.width = value+"%"; 

This confusion is the very reason why JSLint recommends declaring all of your variables at the top of your function: 造成混淆的原因是JSLint建议在函数顶部声明所有变量的原因:

function progress(){
    var min = 0,
        max = 10,
        x = 1,
        i,
        main_div = document.createElement('div'),
        einfuegen,
        einfuegen2,
        zielort,
        minValue = 0,
        maxValue = 100,
        value;

    // rest of function...
}

The variable list above is very long because it has the values for both the outside of the loop and the inside of the loop. 上面的变量列表很长,因为它同时具有循环外部和循环内部的值。 The solution to this is to factor your code out into separate functions: 解决方案是将代码分解为单独的函数:

function progress(){
    var min = 0,
        max = 10;
        x = 1,
        main_div = document.createElement('div'),
        i;

    main_div.className = "main_div";
    document.body.appendChild(main_div);

    for(i = 0; i < x; i += 1){
        mainDiv.appendChild(makeProgressBar());    
    }
}

function makeProgressBar() {
    var einfuegen = document.createElement('div'),
        einfuegen2 = document.createElement('img'),
        min = 0,
        max = 100,
        x = Math.floor(Math.random() * (max - min)) + min;

    einfuegen.className = 'statusbar';

    einfuegen2.id = 'bild';
    einfuegen2.name = 'bild';
    einfuegen2.src = 'project_status.gif';  

    einfuegen.appendChild(einfuegen2);

    einfuegen2.style.width = x+"%"; 

    return einfuegen;
}

This will also help to prevent variable name collisions. 这也将有助于防止变量名冲突。

You need to use some different names for variable in loop 您需要为循环中的变量使用一些不同的名称

var min = 0;
var max = 100;
var x = Math.floor(Math.random() * (max - min)) + min;

it should be 它应该是

var min1 = 0;
var max1 = 100;
var x1 = Math.floor(Math.random() * (max1 - min1)) + min1;

As you are using x in loop condition and modifying it inside loop, causing malfunction of loop. 当您在循环条件中使用x并在循环内部对其进行修改时,会导致循环故障。

x starts as 1 but you change it in the loop like this: x从1开始,但是您可以像这样在循环中更改它:

var x = Math.floor(Math.random() * (max - min)) + min;

which returns a number between 0-100 and the loop continues until it reaches above the random number. 它返回0-100之间的数字,循环继续进行,直到达到随机数以上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM