How to use js setTimeout in double for loop?
代言
代言 2017-06-26 10:54:17
0
2
1006

Numbers that are not printed in the log also take up time. Why and how to solve it?

<script>
function resort(){
    for (let i = 0; i < 10; i++) {
        for (let j = 0; j < 10 - i; j++) {
            setTimeout(function(){
                console.log(i*10 + j);
            },(i*10+j)*500);
        };
    };
}
resort();
</script>
代言
代言

reply all(2)
世界只因有你

What do you want to express?

过去多啦不再A梦

It is also the same as the closure in the for loop, wrapping a layer on the outside to execute the function immediately

function resort(){
    for (let i = 0; i < 10; i++) {
        for (let j = 0; j < 10 - i; j++) {
          (function(a,b){
            setTimeout(function(){
                console.log(a*10 + b);
            },(a*10+b)*500);
          })(i,j)
        };
    };
}
resort();

I don’t know if this is what it means

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template