DanCode
DanCode

Reputation: 673

jquery: calling functions recursively

I wanted to call onIdle() recursively. But when I run it, the function onIdle() is not defined. How can I call onIdle() inside onIdle()?

$(document).idle({
    onIdle: function(){
    
       console.log("number of active jQuery: " + jQuery.active);
    
       if (jQuery.active > 0) {
    
           // add set timeout
           setTimeout(wakeBrowser, 10);
    
           function wakeBrowser() {
               onIdle(); // onIdle is not defined
           }
    
          return;
        }
    }
}

Upvotes: 0

Views: 44

Answers (1)

Kevin Perry
Kevin Perry

Reputation: 541

Just name the function when you define it, then use that name to call it recursively:

$(document).idle({
    onIdle: function myIdleFunc(){
    
       console.log("number of active jQuery: " + jQuery.active);
    
       if (jQuery.active > 0) {
    
           // add set timeout
           setTimeout(wakeBrowser, 10);
    
           function wakeBrowser() {
               myIdleFunc();
           }
    
          return;
        }
    }
}

Upvotes: 2

Related Questions