如果我在内部函数上调用onload事件,是否考虑递归? [英] Does it consider recursion if I call onload event on the funcion which I'm inside?

查看:124
本文介绍了如果我在内部函数上调用onload事件,是否考虑递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

  function lazyCss(gallery) {
    var images = gallery.slider.find('[data-lazy-image]'),
    imageLinks = [],
    imageToLoad = -1;

    for (var i = 0; i < images.length; i++) {
      imageLinks[i] = images.eq(i).data('lazy-image');
      images.eq(i).removeAttr('data-lazy-image');
    }

    function loadImage() {
      imageToLoad++;
      images.eq(imageToLoad).css('background-image', 'url(' + imageLinks[imageToLoad] + ')');
      if (imageToLoad != images.length - 1) {
        $('<img/>').attr('src', imageLinks[imageToLoad]).on('load', loadImage);
      }
    }

    loadImage();
  }

我的问题是:

1)当我将事件处理程序"loadImage"附加到图像时,由于我在此函数中执行此操作,它是否考虑了递归?

1) When I attach an event handler "loadImage" to an image, does it consider recursion because I'm doing it inside this function?

我担心此代码将不利于我的网站性能,因为递归被认为是不好的.

My worries that this code will be bad for performance on my website, since recursion is considered bad.

推荐答案

我的问题是:

My questions is:

1)当我将事件处理程序"loadImage"附加到图像时,执行该操作 考虑递归,因为我是在此函数中执行的?

1) When I attach an event handler "loadImage" to an image, does it consider recursion because I'm doing it inside this function?

否,问题JavaScript不执行递归".您正在计划可能的函数调用,而不是return从函数调用中获取值.

No, the JavaScript at Question does not perform a "recursion". You are scheduling a possible function call, not returning a value from a function call.

请参见

在JavaScript中,递归",偶然引用自己的非终止过程"和重复调度"之间的区别是什么?

这篇关于如果我在内部函数上调用onload事件,是否考虑递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆