2009-09-19 88 views
1

我正在使用jQuery来侦听DOMSubtreeModified事件,然后执行一个函数。我需要的是一种只在每个事件爆发时运行一次功能的方法。所以在这种情况下,事件将仅在1秒后运行,并且在3秒后再次运行。做这个的最好方式是什么?

使用jQuery为每个事件突发运行一次函数

jQuery的

$(function(){ 

    setTimeout(function(){ 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
    },1000); 

    setTimeout(function(){ 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
    },3000); 

    $('#container').bind('DOMSubtreeModified',function(){ 
     console.log('event'); 
     functionToRun(); 
    }); 

}); 

HTML

<div id="container"></div> 


更新
setTimeout函数的存在只是为了模仿我的问题。我需要一个解决方案,而无需更改setTimeout代码。我遇到的问题是我得到了突发的DOMSubtreeModified事件,并且每次爆发只需要一次。

+0

如果我理解正确,'setTimeout'的东西不是你所问的。你想知道如何修改元素6次,但是'functionToRun'只运行一次,是否正确? – 2009-09-19 14:32:33

+0

是:) – 2009-09-19 14:37:43

回答

1

解决它我。

$(function(){ 

    setTimeout(function(){ 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
    },1000); 

    setTimeout(function(){ 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
    },1100); 

    setTimeout(function(){ 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
     $('#container')[0].innerHTML = 'test'; 
    },3000); 

    addDivListener(); 

}); 

function addDivListener() { 
    $('#container').bind('DOMSubtreeModified',function(){ 
     functionToRun(); 
     $(this).unbind('DOMSubtreeModified'); 
     setTimeout(addDivListener,10); 
    }); 
} 

function functionToRun(){ 
    console.log('event'); 
} 

此打印出事件 3次在Firebug控制台,并精确到100毫秒。

0

这似乎是你在找什么:

$(
    function() 
    { 
    setTimeout 
    (
     StartWatching, 
     1000 
    ); 
    } 
); 

function StartWatching() 
{ 
    $('#container') 
    .bind(
      'DOMSubtreeModified', 
      function() 
      { 
       console.log('event'); 
       StopWatchingAndStartAgainLater(); 
      } 
     ); 
} 

function StopWatching() 
{ 
    $('#container') 
     .unbind('DOMSubtreeModified'); 
} 

function StopWatchingAndStartAgainLater() 
{ 
    StopWatching(); 
    setTimeout 
    (
    StartWatching, 
    3000 
); 
} 

这加强了以下流程:

Document.Ready 
Create DOM watching event after one second 
On event, turn off event and recreate it after three seconds, rinse, repeat 
+0

语法错误,如果我修复它,它仍然只打印出1个事件。 – 2009-09-19 16:04:42

+0

@mofle ...对setTimeout函数的调用做了一些细微的改动。我会在本地测试它来验证这个工作。 – 2009-09-19 16:25:29

+0

@mofle ...确认上述工作时,我将StartWatching更改为简单附加到div并拨打StopWatchingAndStartAgainLater。如果这仍然失败,那么有可能是某些东西不适用于绑定到DOMSubtreeModified事件。 – 2009-09-19 16:30:14

0

尝试使用one()处理

documentation

+0

对不起,我不能详细说明并重写你的代码,我很着急。但如果你无法弄清楚,我可以稍后再做。 – 2009-09-19 14:38:57

+0

已经尝试过了,但它只给我一个事件,从第一个事件爆发(第一个超时)开始,第二个爆发没有任何事件。 – 2009-09-19 14:39:21

3

替代方法,它将控制任何函数的速率。

// Control the call rate of a function. 
// Note that this version makes no attempt to handle parameters 
// It always induces a delay, which makes the state tracking much easier. 
// This makes it only useful for small time periods, however. 
// Just clearing a flag after the timeout won't work, because we want 
// to do the the routine at least once if was called during the wait period. 
throttle = function(call, timeout) { 
    var my = function() { 
    if (!my.handle) { 
     my.handle = setTimeout(my.rightNow, timeout); 
    } 
    }; 
    my.rightNow = function() { 
    if (my.handle) { 
     clearTimeout(my.handle); 
     my.handle = null; 
    } 
    call(); 
    }; 
    return my; 
}; 
+0

谢谢!在我的Chrome扩展程序中使用此功能(https://github.com/yuvipanda/yenWikipedia)。 – Yuvi 2011-04-28 22:40:18

相关问题