简体   繁体   English

JavaScript关闭回调本身

[英]JavaScript Closure Callback Itself

Here is the closure I wrote: 这是我写的闭包:

var slider = (function(){
    var count = 3;
    return function(){
        if (count>0) {
            $("#message").html("<h1>"+count+"</h1>");
            count--;
            $("#message").removeClass("animated zoomIn");
            $("#message").addClass("animated zoomIn");
            $("#message").one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend",  function(){
                return slider();
            }());   
        } else{
            showCube();
        }
    }()
});

This closure gets executed by a jquery plugin: 这个闭包由一个jQuery插件执行:

$("#fishes").addClass("animated slideOutLeft").one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend", slider());

However, the callback is not getting executed. 但是,回调未得到执行。 Can someone guide me in the correct direction please? 有人可以指引我正确的方向吗?

The way I read it, you want slider() to return a closure you can use for one . 按照我的阅读方式,您希望slider()返回一个可以用于one的闭包。 However - it doesn't, as your code suffers from the premature execution syndrome. 但是,事实并非如此,因为您的代码遭受过早执行综合症的困扰。

Your slider function creates a closure, then immediately executes it , then returns the result of that execution. 您的slider函数创建一个闭包,然后立即执行它 ,然后返回该执行的结果。 Since the inner closure has no return statements, the value returned from slider() is undefined , and one doesn't get any handler to attach. 由于内罩没有return语句,从返回的值slider()undefined ,而one没有得到任何处理程序连接。

Solution: remove the () from the second-to-last line. 解决方案:从倒数第二行删除() That should make slider() return a closure rather than its result. 那应该使slider()返回一个闭包而不是它的结果。

EDIT: Your function() { return slider(); }() 编辑:您的function() { return slider(); }() function() { return slider(); }() is equivalent to slider() , which constructs a new closure where count is 3 . function() { return slider(); }()等效于slider() ,后者构造一个新的闭包,其中count3 Replace it with arguments.callee , to provide the same closure that is executing right now. 将其替换为arguments.callee ,以提供与当前正在执行的闭包相同的闭包。

 var slider = (function(){ var count = 3; return function(){ if (count>0) { $("#message").html("<h1>"+count+"</h1>"); count--; $("#message").one("click", arguments.callee); } else{ alert("The End"); } } }); $('#message').one('click', slider()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="message">Click me!</div> 

Or, you can name it, so you can reference it directly: 或者,您可以命名它,以便直接引用它:

 var slider = (function(){ var count = 3; return function sliderClosure(){ if (count>0) { $("#message").html("<h1>"+count+"</h1>"); count--; $("#message").one("click", sliderClosure); } else{ alert("The End"); } } }); $('#message').one('click', slider()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="message">Click me!</div> 

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

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