Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
});
like image 708
Richard Knop Avatar asked Sep 02 '09 16:09

Richard Knop


People also ask

What is a recursive function in JS?

Recursion is a process of calling itself. A function that calls itself is called a recursive function. The syntax for recursive function is: function recurse() { // function code recurse(); // function code } recurse(); Here, the recurse() function is a recursive function.

What is recursive function example?

Simple examples of a recursive function include the factorial, where an integer is multiplied by itself while being incrementally lowered. Many other self-referencing functions in a loop could be called recursive functions, for example, where n = n + 1 given an operating range.

Does JS have recursive function?

Introduction to the JavaScript recursive functions The recurse() is a recursive function if it calls itself inside its body, like this: function recurse() { // ... recurse(); // ... } Generally, you use recursive functions to break down a big problem into smaller ones.


5 Answers

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

arguments.callee( .... );

See here for more info.

like image 147
jimr Avatar answered Oct 02 '22 14:10

jimr


The top answer is out of date. Currently (Aug 2012) callee is deprecated at least in Firefox.Using callee is out of date. Currently (Aug 2012) callee is "... deprecated 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 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:

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!

$('.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.

$('.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 );
});
like image 29
Allen Oliver Avatar answered Oct 02 '22 14:10

Allen Oliver


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();});
like image 30
krdluzni Avatar answered Sep 30 '22 14:09

krdluzni


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

// here I want to call the function again
arguments.callee();
like image 30
Paul Dixon Avatar answered Sep 29 '22 14:09

Paul Dixon


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

(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.

like image 24
Matt Avatar answered Sep 30 '22 14:09

Matt