💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ // for (let i = 0; i <= 5; i++) { // setTimeout(function () { // console.log(i); // }, 1000); // } ~~~ 打印6,过1秒连续打印012345 ![](https://img.kancloud.cn/7e/2b/7e2bf1582041bad000882b101504fab2_440x273.png) ~~~ // for (var i = 0; i <= 5; i++) { // ~function (i) { // setTimeout(function () { // console.log(i); // }, 1000); // }(i); // } ~~~ 打印-1,过1秒连续打印012345 ![](https://img.kancloud.cn/3d/47/3d47f0224e8aca02a4350625f9f042ec_390x307.png) ~~~ // for (var i = 0; i <= 5; i++) { // setTimeout(function (i) { // return function () { // console.log(i); // } // }(i), 1000); // } ~~~ 打印6,过1秒连续打印012345 ![](https://img.kancloud.cn/4d/83/4d83e55a30e5a54baebecdb98ea9a4d4_416x305.png) ~~~ // for (var i = 0; i <= 5; i++) { // setTimeout((i => () => console.log(i))(i), 1000); // } ~~~ 打印6,过1秒连续打印012345 ![](https://img.kancloud.cn/f8/80/f88065f24c41eae053e4a1b2b4b8d30e_463x237.png) ~~~ // for (var i = 0; i <= 5; i++) { // setTimeout(function (i) { // console.log(i); // }.bind(null, i), 1000); // } //... ~~~ 打印6,过1秒连续打印012345 ![](https://img.kancloud.cn/ab/3d/ab3da10d319222b96569fb6da039cc96_388x274.png) ~~~ for(var i=0;i<5;i++){ setTimeout(function () { console.log(new Date(),i);//5,5,5,5,5 },1000); } console.log(new Date(),i); //直接输出5,过一秒后输出5个5 for(var i=0;i<5;i++){ setTimeout(() => { console.log(new Date(),i);//5,5,5,5,5 },1000); } console.log(new Date(),i); //直接输出5,过一秒后输出5个5 for(var i=0;i<5;i++){ (function (j) { setTimeout(function () { console.log(new Date(),j); },1000); })(i); } console.log(new Date(),i); //开始输出5,过一秒后直接输出0,1,2,3,4 var output = function (i) { setTimeout(function () { console.log(new Date(),i); },1000); }; for(var i=0;i<5;i++){ output(i); } console.log(new Date(),i) //开始输出5,过一秒后直接输出0,1,2,3,4 for(let i=0;i<5;i++){ setTimeout(function () { console.log(new Date(),i); },1000); } console.log(new Date(),i); //开始输出5,过一秒后直接输出0,1,2,3,4 for(var i=0;i<5;i++){ (function (j) { setTimeout(function () { console.log(new Date(),j); },1000*j); })(i); } //每隔一秒输出0,1,2,3,4 setTimeout(function () { console.log(new Date(),i); },1000*i); //隔5秒输出5 const tasks = []; for(var i=0;i<5;i++){ ((j)=>{ tasks.push(new Promise((resolve)=>{ setTimeout(()=>{ console.log(new Date(),j); resolve(); },1000*j); })); })(i); } //每隔一秒输出0,1,2,3,4 Promise.all(tasks).then(()=>{ setTimeout(()=>{ console.log(new Date(),i); },1000) }); //隔5秒输出5 const tasks = []; const output = (i) => new Promise((resolve)=>{ setTimeout(()=>{ console.log(new Date(),i); resolve(); },1000*i); }); for(var i=0;i<5;i++){ tasks.push(output(i)); } //每隔一秒输出0,1,2,3,4 Promise.all(tasks).then(()=>{ setTimeout(()=>{ console.log(new Date(),i); },1000) }); //隔5秒输出5 //ES7 const sleep = (timeountMS)=> new Promise((resolve)=>{ setTimeout(resolve,timeountMS); }); (async()=>{ for(var i=0;i<5;i++){ await sleep(1000); console.log(new Date(),i); } await sleep(1000); console.log(new Date(),i); })(); //每隔一秒输出0,1,2,3,4,5 ~~~