简体   繁体   English

addEventListener创建了一个无限循环

[英]addEventListener Created an Infinite Loop

I added an event listener and it created an infinite loop I really don't get what I did wrong. 我添加了一个事件监听器,它创建了一个无限循环,我真的不知道我做错了什么。

What happen is it keeps clicking the images by itself 它会不断地单击图像

Here's my code: 这是我的代码:

function attachOnClickEvent(cases,theme,debut,capture){
    var images=document.getElementsByTagName('img');
    for(var i=0;i<images.length;i++){
        images[i].addEventListener("click",x(cases,theme,debut,capture),false);
    }
};

var x=function(cases,theme,debut,capture){newImage(this,cases,theme,debut,capture);};

function newImage(elem,cases,theme,debut,capture){
    var images=preloadImages(cases,theme);
    var imgTab=document.getElementsByTagName('img');
    var Id=elem.id;
    elem.src=images[randomTab[Id]];

}; 

Hope someone can find my mistake.. Thanks! 希望有人能找到我的错误..谢谢!

  1. getElementsByTagName is returning a live NodeList getElementsByTagName返回一个实时NodeList
  2. You are invoking your function x each time instead of adding it as a handler 您每次调用函数 x而不是将其添加为处理程序
  3. A new <img> is created 创建了一个新的<img>
  4. the NodeList has it's length increased by 1 NodeList的长度增加了1
  5. The next iteration is the same distance away from the end as the previous one 下一次迭代与前一次迭代的距离与前一次相同

There are two things you need to consider, firstly as I mention in 2 , you're invoking x when you aren't meaning to. 有两件事你需要考虑,首先,正如我在2提到的那样,当你没有意义时,你正在调用x Secondly, you may avoid some problems with live lists by looping downwards 其次,您可以通过向下循环来避免实时列表的一些问题


One fix to get your desired result may be to rewrite attachOnClickEvent like this 获得所需结果的一个方法是重写像这样的attachOnClickEvent

function attachOnClickEvent(cases, theme, debut, capture) {
    var images = document.getElementsByTagName('img'),
        handler = function (e) {
            return x(cases, theme, debut, capture);
        },
        i;
    for (i = images.length - 1; i >= 0; --i) {
        images[i].addEventListener("click", handler, false);
    }
}

One issue would seem to be that you are not using callbacks properly. 一个问题似乎是您没有正确使用回调。

Callbacks need to be function objects and cannot be returned as you are intending. 回调需要是函数对象,不能按照您的意图返回。

function (cases, theme, debut, capture) {
    return function() { newImage(this, cases, theme, debut, capture); };
}

Try returning an anonymous function. 尝试返回匿名函数。

Would be better to see how you're calling these functions, but I noticed that on this line: 会更好地了解你如何调用这些函数,但我注意到这一行:

images[i].addEventListener("click",x(cases,theme,debut,capture),false);

You are calling your function x , rather than assigning it as the event listener. 您正在调用函数x ,而不是将其指定为事件侦听器。 Since that function adds an image to the page, the loop never completes, since it iterates through all images on the page. 由于该函数会向页面添加图像,因此循环永远不会完成,因为它会遍历页面上的所有图像。 This line should instead be: 这一行应该是:

images[i].addEventListener("click", x , false );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM