简体   繁体   English

JQuery递归函数?

[英]JQuery recursive function?

How can I call a function from inside the function, so it becomes recursive? 如何从函数内部调用函数,使其变为递归? Here is my code, I have added a comment where I would like to start the recursion: 这是我的代码,我添加了一个注释,我想开始递归:

$('a.previous-photos, a.next-photos').click(function() {
    var id = $('#media-photo img').attr('id');
    var href = $(this).attr('href');
    href = href.split('/');
    var p = href[href.length - 1];
    var url = '/view/album-photos/id/' + id + '/p/' + p;

    $.get(url, function(data) {
        $('.box-content2').replaceWith('<div class="box-content2"' + data + '</div>');
    });

    // here I want to call the function again

    return false;
});

You can make a recursive call to an anonymous function by doing 您可以通过执行对匿名函数的递归调用

arguments.callee( .... );

See here for more info. 有关详细信息,请参见此处

The top answer is out of date. 最佳答案已过时。 Currently (Aug 2012) callee is deprecated at least in Firefox.Using callee is out of date. 目前(2012年8月)callee至少在Firefox中被弃用。使用被调用者已过时。 Currently (Aug 2012) callee is "... deprecated by ECMA-262."(see discussion ) 目前(2012年8月)被调用者“......被ECMA-262弃用。”(见讨论

There are two problems you are running into: 您遇到两个问题:

  1. the function handler will only be passed the event object. 函数处理程序只会传递事件对象。
  2. the function is not named, so you can't refer to it for recursion 该函数未命名,因此您无法引用它进行递归

Solution for 2: 解决方案2:

This is the easier of the two. 这是两个中更容易的。 Typically the reason for using anonymous functions is to keep a namespace clean. 通常,使用匿名函数的原因是保持名称空间清洁。 Parentheses define a local namespace, so after giving the function a name it will not be accessible outside the parentheses. 括号定义了一个本地命名空间,因此在给函数命名后,它将无法在括号外访问。 The following will work for you: 以下内容适合您:

$('.someclass').onClick( function dosomething(){
    ... your code ...
    dosomething() //again
});
dosomething() // will cause scope error, function not defined

Solution for 1: 解决方案1:

This is a little more difficult. 这有点困难。 Since the only thing passed to the function is the event object you will need to extend that to pass in values. 由于唯一传递给函数的是事件对象,因此需要对其进行扩展以传入值。 Fortunately, it turns out that jQuery has a system just for this ! 幸运的是,事实证明,jQuery有一个系统, 只是这个

$('.someclass').on( 'click', {myvar: 0}, function dosomething(event){
    ... your code ...
    event.data.myvar = event.data.myvar + 1;
    dosomething(event) //again
});

Note: this is especially useful for when you must attach and detach a handler to prevent inifinite loops like with DOMSubtreeModified. 注意:当您必须附加和分离处理程序以防止像DOMSubtreeModified这样的无限循环时,这尤其有用。

$('.someclass').on( 'DOMSubtreeModified.mynamespace', {myvar: 0}, function myfunc( event ){
    $(this).off( 'DOMSubtreeModified.mynamespace' );
    ... Some Code that changes .someclass subtree ...
    event.data.myvar = event.data.myvar + 1;
    $(this).on( 'DOMSubtreeModified.mynamespace', {myvar: event.data.myvar}, myfunc );
});

Something of this sort should do the trick, but there ought to be a nicer way to set it up: 这种东西应该可以解决问题,但应该有一个更好的方法来设置它:

function myfunc() {
    var id = $('#media-photo img').attr('id');
    var href = $(this).attr('href');
    href = href.split('/');
    var p = href[href.length - 1];
    var url = '/view/album-photos/id/' + id + '/p/' + p;

    $.get(url, function(data) {
        $('.box-content2').replaceWith('<div class="box-content2"' + data + '</div>');
    });

    if(!cond){//you need a condition, or it'll recurse indefinitely.
       myfunc();
    }

    return false;
}

$('a.previous-photos, a.next-photos').click(function(){myfunc();});

From Javascript 1.2 onwards you can use arguments.callee(...) to effect a recursive call to an anonymous function 从Javascript 1.2开始,您可以使用arguments.callee(...)来实现对匿名函数的递归调用

// here I want to call the function again
arguments.callee();

Put your code in a jQuery plugin format and call itself for example... 把你的代码放在一个jQuery插件格式中,并调用自己为例...

(function($) {
$.fn.togglethis = function () {
    $(this).animate({opacity:"1.0"}, 1000, function() {
        /* Code Here */
        return $(this);
    });

}
})(jQuery);
$(document).ready(function() {
    $("#togglethis").togglethis();
});

Insert your desired code where the comment is. 在评论所在的位置插入所需的代码。

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

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