简体   繁体   English

jquery:递归调用函数

[英]jquery: calling functions recursively

I wanted to call onIdle() recursively.我想递归调用 onIdle() 。 But when I run it, the function onIdle() is not defined.但是当我运行它时,没有定义 function onIdle() 。 How can I call onIdle() inside onIdle()?如何在 onIdle() 中调用 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;
        }
    }
}

Just name the function when you define it, then use that name to call it recursively:定义时只需命名 function,然后使用该名称递归调用它:

$(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;
        }
    }
}

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

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