简体   繁体   English

如何在jQuery中为多个函数进行无限循环

[英]how to make infinite loop for more than one function in jquery

this is my jquery code.this code contain three functions.this three function repeatedly execute for looping.but this code not run properly.how to make recursive call with three functions.the pid1,pid2,pid3 is 这是我的jquery代码。此代码包含三个函数。这三个函数反复执行以循环执行。但是此代码无法正常运行。如何使用三个函数进行递归调用。pid1,pid2,pid3为

tag id's. 标签ID。

     $(document).ready(function(){
     function animate() 
       {    
         $('#pid1').fadeOut(3000, function()
        {
         $(this).text('string1').fadeIn(3000);
        }); 
        animate1();
       }
     function animate1()
      {
       $('#pid2').fadeOut(3000, function()
       {
       $(this).text('string2').fadeIn(3000);
       });
       animate2();
      }
    function animate2() 
      {
       $('#pid3').fadeOut(3000, function()
       {
       $(this).text('string3').fadeIn(3000);
       });      
       animate();   
      }
   });

I want to share one pattern here: 我想在这里分享一种模式:

var observer = {
  list: {},
  add: function(id, item){this.list[id] = item},
  del: function(id){delete this.list[id];},
  announce: function(data){
    for(var id in this.list){
      setTimeout(function(){
        this.list[id](data);
      }, 0);
    }
  }
}

Usage: 用法:

observer.add('first', function(data){
  console.log('first', data);
})

observer.add('second', function(data){
  console.log('second', data);
})

var i = 0;
setInterval(function(){observer.announce(++i);}, 300);

You can to miss data parameter, or use all arguments then announce you subscibers... 您可以错过数据参数,或使用所有arguments然后宣布您属于子级...
This pattern equals how events work 这种模式等于事件如何工作

I'm sorry, that I misunderstood question... 抱歉,我误解了问题...
Here is correct code: 这是正确的代码:

function foo1(){
  console.log(1);
  setTimeout(foo2, 1000);
}

function foo2(){
  console.log(2);
  setTimeout(foo1, 1000);
}

window.onload = foo1;
function a() {
    /* stuff */ 
}
function b() {
    /* stuff */
}

var functionArray = [a(), b()];

$.each(functionArray, function(key, value) {

/* extra stuff while looping through functions */

});

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

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