代码-S之闭包循环输出1-100

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<script>
    // 间隔1秒,依次输出12345
    /*for(var i=1; i<=5; i++){
        (function(x){
            setTimeout(function(){
                console.log(x);
            }, 1000*x);
        })(i);
    }*/
    //在1秒钟(1000毫秒)之内,要求输出1~100
    //平均每隔10毫秒,输出一个数
    for(var i=1; i<=100; i++){
        (function(x){
            setTimeout(function(){
                console.log(x);
            }, 10*x);
        })(i);
    }
</script>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_39723600/article/details/83120452