[ACCEPTED]-JQuery recursive function?-jquery

Accepted answer
Score: 10

You can make a recursive call to an anonymous 1 function by doing

arguments.callee( .... );

See here for more info.

Score: 6

The top answer is out of date. Currently 17 (Aug 2012) callee is deprecated at least 16 in Firefox.Using callee is out of date. Currently 15 (Aug 2012) callee is "... deprecated 14 by ECMA-262."(see discussion)

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:

This is the easier 13 of the two. Typically the reason for using 12 anonymous functions is to keep a namespace 11 clean. Parentheses define a local namespace, so 10 after giving the function a name it will 9 not be accessible outside the parentheses. The 8 following will work for you:

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

Solution for 1:

This is a little 7 more difficult. Since the only thing passed 6 to the function is the event object you 5 will need to extend that to pass in values. Fortunately, it 4 turns out that jQuery has a system just for this!

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

Note: this 3 is especially useful for when you must attach 2 and detach a handler to prevent inifinite 1 loops like with 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 );
Score: 3

Something of this sort should do the trick, but 2 there ought to be a nicer way to set it 1 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.

    return false;

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

From Javascript 1.2 onwards you can use 2 arguments.callee(...) to effect a recursive call to an anonymous 1 function

// here I want to call the function again
Score: 1

Put your code in a jQuery plugin format 2 and call itself for example...

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

$(document).ready(function() {

Insert your 1 desired code where the comment is.

More Related questions