函数调用自身不起作用(无限循环,Javascript)
我正在尝试等待,然后在数组中的所有图像完成加载(使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除函数调用,并去掉引号。如果您不加引号,
setTimeout
将获得对稍后可以调用的函数的直接引用。但是,如果在诸如"checkForAllImagesLoaded"
或"checkForAllImagesLoaded()"
之类的字符串中,则在超时时它将执行传入的代码。那时,将在全局对象(窗口)中搜索
checkForAllImagesLoaded
但它并未在那里定义,原因是您收到undefined
错误。您的代码包装在自调用匿名函数中,并且在其外部不存在
checkForAllImagesLoaded
。因此,在 setTimeout 调用中传递对函数的直接引用,而不是字符串。setTimeout
可以使用函数(和可选参数),或包含 JavaScript 代码的字符串: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 theundefined
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
can be called with either a function (and optional arguments), or a string containing JavaScript code:删除 settimeout 调用中的 ()。
setTimeout('checkForAllImagesLoaded', 20);
Remove the () in the settimeout call.
setTimeout('checkForAllImagesLoaded', 20);
使用您的代码,您可以设置每次调用的超时次数。您应该只为每次
checkForAllImagesLoaded()
调用设置一次超时,并可能增加等待时间(20 毫秒太快了)。例如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.