函数调用自身不起作用(无限循环,Javascript)

发布于 2024-09-07 18:24:51 字数 749 浏览 10 评论 0原文

我正在尝试等待,然后在数组中的所有图像完成加载(使用 .complete)时收到消息,根据答案 此处。因此,我设置了一个无限循环,如下所示。但是,当我运行此命令时,出现错误,指出 checkForAllImagesLoaded() 未定义。该代码通过一个小书签运行,因此它全部包含在一个匿名函数构造中(如下所示)。如果我在该构造之外重新定义函数和变量,它就会起作用。但这似乎是编写小书签的一种糟糕方法。我该如何解决这个问题,以便在 setTimeout 之后它仍然可以识别该函数?

(function() {

    //var images = array of images that have started loading

    function checkForAllImagesLoaded(){
        for (var i = 0; i < images.length; i++) {
            if (!images[i].complete) {
               setTimeout('checkForAllImagesLoaded()', 20);
               return;
            }
        }
    }

    checkForAllImagesLoaded();

})();

I'm trying to wait and then get a message when all images in an array have completed loading (using .complete), per the answer here. As such I set up an infinite loop like the below. however, when I run this I get an error that checkForAllImagesLoaded() is not defined. This code is being run through a bookmarklet, and as such it's all wrapped up in an anonymous function construct (as below). If I re-define my function and variable outside of that construct, it works. But that seems to be a poor way to write a bookmarklet. How can I fix this so it will still recognize the function after the setTimeout?

(function() {

    //var images = array of images that have started loading

    function checkForAllImagesLoaded(){
        for (var i = 0; i < images.length; i++) {
            if (!images[i].complete) {
               setTimeout('checkForAllImagesLoaded()', 20);
               return;
            }
        }
    }

    checkForAllImagesLoaded();

})();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不即不离 2024-09-14 18:24:51

删除函数调用,并去掉引号。如果您不加引号,setTimeout 将获得对稍后可以调用的函数的直接引用。但是,如果在诸如 "checkForAllImagesLoaded""checkForAllImagesLoaded()" 之类的字符串中,则在超时时它将执行传入的代码。

那时,将在全局对象(窗口)中搜索 checkForAllImagesLoaded 但它并未在那里定义,原因是您收到 undefined 错误。

您的代码包装在自调用匿名函数中,并且在其外部不存在 checkForAllImagesLoaded 。因此,在 setTimeout 调用中传递对函数的直接引用,而不是字符串。

setTimeout(checkForAllImagesLoaded, 20);

setTimeout 可以使用函数(和可选参数),或包含 JavaScript 代码的字符串:

var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);

Remove the function call, and take out the quotes. If you don't put the quotes, setTimeout gets a direct reference to the function which it can invoke later. However, if inside a string such as "checkForAllImagesLoaded" or "checkForAllImagesLoaded()", then it will execute the code passed-in when the timeout occurs.

At that time, checkForAllImagesLoaded will be searched for in the global object (window) but it is not defined there, reason being why you're getting the undefined error.

Your code is wrapped in a self-calling anonymous function, and outside of it checkForAllImagesLoaded does not exist. So pass a direct reference to the function in your setTimeout call, instead of a string.

setTimeout(checkForAllImagesLoaded, 20);

setTimeout can be called with either a function (and optional arguments), or a string containing JavaScript code:

var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);
清君侧 2024-09-14 18:24:51

删除 settimeout 调用中的 ()。

setTimeout('checkForAllImagesLoaded', 20);

Remove the () in the settimeout call.

setTimeout('checkForAllImagesLoaded', 20);

清风夜微凉 2024-09-14 18:24:51

使用您的代码,您可以设置每次调用的超时次数。您应该只为每次 checkForAllImagesLoaded() 调用设置一次超时,并可能增加等待时间(20 毫秒太快了)。例如

function checkForAllImagesLoaded() {
  var allComplete=true;
  var i=0;

  while (i<images.length && allComplete) {
    allComplete=images[i++].complete;
  }

  if (!allComplete) { // Any incomplete images?
    setTimeout('checkForAllImagesLoaded()',1000); // Wait a second!
  }
}

With your code, you set a number of timeouts per call. You should just set the timeout once per checkForAllImagesLoaded() call and perhaps increase the waiting period (20 milliseconds is just too quick). E.g.

function checkForAllImagesLoaded() {
  var allComplete=true;
  var i=0;

  while (i<images.length && allComplete) {
    allComplete=images[i++].complete;
  }

  if (!allComplete) { // Any incomplete images?
    setTimeout('checkForAllImagesLoaded()',1000); // Wait a second!
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文