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

轮播图中的一个疑问 #4

Open
zzjd opened this issue Feb 15, 2020 · 1 comment
Open

轮播图中的一个疑问 #4

zzjd opened this issue Feb 15, 2020 · 1 comment

Comments

@zzjd
Copy link

zzjd commented Feb 15, 2020

请问轮播图JS代码中
for(var i = 0; i < 4; i++){
(function (j) {
li[j].addEventListener('click',function () {
clearInterval(timer);
index = j;
moveTo(index);
timer = setInterval(disPlay,2000);
})
}(i))
}
应该如何理解呢?j是如何来的,以及“(i)”的意义是什么呢?

@facsli1997
Copy link

立即执行函数,把i传给j。轮播图是给每一个li添加点击事件,点击事件是延迟执行,而for循环是立即执行。如果没有立即执行函数(function(j){//******})(i),当点击事件触发时,for循环已经结束了,每一个的index已经变成2了。通过立即执行函数及时地把i传进去。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants