0

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;
        }
    }
}
2
  • 2
    Consider creating the function outside the idle( call, and call it onIdle and then pass it to the idle call idle(onIdle)? Commented Jan 25, 2022 at 20:01
  • It's not recursive unless you are calling the function within the function Commented Jan 25, 2022 at 22:56

1 Answer 1

2

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.