Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

闭包的应用 #30

Open
hsipeng opened this issue Feb 26, 2018 · 0 comments
Open

闭包的应用 #30

hsipeng opened this issue Feb 26, 2018 · 0 comments

Comments

@hsipeng
Copy link
Owner

hsipeng commented Feb 26, 2018

顺序输出1~5

for (var i=1; i<=5; i++) {
(function(b){
return setTimeout( function timer() {
console.log(b);
}, b*1000 )
})(i)
}

另外集中方式

1.利用setTimeout第三个参数
for (var i=1; i<=5; i++) {
setTimeout( function timer(i) {
console.log(i);
}, i*1000,i );
}
2.利用bind方法
for (var i=1; i<=5; i++) {
setTimeout( function timer(i) {
console.log(i);
}.bind(null,i), i*1000 );
}
3.利用let
for (let i=1; i<=5; i++) {
setTimeout( function timer() {
console.log(i);
}, i*1000 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant