简体   繁体   English

JavaScript“函数工厂”似乎不起作用

[英]“Function factory” doesn't seem to work, JavaScript

I'm displaying a series of images in a loop, and I'm trying to implement some sort of nudity filter so I'm using nude.js , a library that can somewhat detect nudity. 我正在循环显示一系列图像,并且尝试实现某种裸体过滤器,因此我使用的是nude.js ,这是一个可以检测裸体的库。 Here's the code: 这是代码:

// we're inside a loop
$(".images").prepend($("<img>").attr({src: whatever, id: uniqueid}).load(function(e) {
    nude.load(e.target.id);
    nude.scan(function(result) { if (!result) $(e.target).detach(); });
});

However, it detaches all of the wrong images because nude.js is slow and it completes after the loop has gone on to the later iterations, detaching those images instead of the one it was working on. 但是,它分离所有错误的图像,因为nude.js速度很慢,并且在循环进行到以后的迭代之后完成,从而分离了这些图像,而不是其正在处理的图像。

I've tried using a function factory: 我试过使用函数工厂:

function generateCallback(arg) {
    return function(result) { if (!result) $(arg).detach(); };
}

and

nude.scan( generateCallback(e.target) )

but the same thing happens. 但同样的事情发生了。

What I want is a load event that will remove the image if it seems to contain nudity. 我想要的是一个load事件,如果它似乎包含裸露内容,它将删除该图像。 How can I do this properly? 如何正确执行此操作?

EDIT: nude.js works like this: 编辑:nude.js是这样的:

nude.load(imageid);
nude.scan(callback); // it'll pass true or false into the callback

another edit: accidentally omitted the id setting from the code I posted, but it was there in my real code, so I added it here. 另一个编辑:意外地从我发布的代码中省略了id设置,但是它在我的真实代码中,所以我在这里添加了它。

I suspect the case here is that this kind of sequential processing won't work with nude.js. 我怀疑这里的情况是,这种顺序处理不适用于nude.js。

Looking at the nude.js code , I think your problem is occurring in the call to nude.scan . 看一下nude.js 代码 ,我认为您的问题出在对nude.scan的调用中。 nude.js has a variable that stores the function to invoke after the scan has completed. nude.js有一个变量,用于存储扫描完成后要调用的函数。 When calling nude.scan(callback) , this variable is set to be callback . 调用nude.scan(callback) ,此变量设置为callback

From your PasteBin , it seems as though the callback gets assigned as expected on the first call, but on the second and subsequent calls, it gets replaced, hence why the second image is detached and not the first. 从您的PasteBin中 ,似乎在第一个调用中按预期分配了回调,但是在第二个及后续调用中,它被替换了,因此为什么要分离第二个而不是第一个图像。

What happends to your script, is that the e var is global to the function and so after each loop it gets replaced with the new one. 您的脚本发生的事情是e var对函数而言是全局的,因此在每次循环后都将其替换为新循环。 So when the first image is scanned, e already became the event of the second image, which get detached. 因此,当扫描第一个图像时, e已经成为第二个图像的事件,该事件被分离。

To solve your problem, use closures. 要解决您的问题,请使用闭包。 If you want to know more about closures, have a look here . 如果您想进一步了解闭包,请在这里查看 Otherway, here's the solution to your problem : 否则,这是您的问题的解决方案:

$(".images").prepend($("<img>").attr({src: whatever, id: uniqueid}).load(function(e) {
    (function(e) {
        nude.load(e.target.id);
        nude.scan(function(result) { if (!result) $(e.target).detach(); });
    }) (e);
});

EDIT: AS nick_w said, there is var that contains the callback and probably gets replaced each time so this is why it isn't the right picture getting detached. 编辑:正如nick_w所说,有一个包含回调的var,每次都可能被替换,因此这就是为什么分离正确图片的原因。 You will probably have to modify the script yourself 您可能必须自己修改脚本

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

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