我正在使用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事件,并且每次爆发只需要一次。
如果我理解正确,'setTimeout'的东西不是你所问的。你想知道如何修改元素6次,但是'functionToRun'只运行一次,是否正确? – 2009-09-19 14:32:33
是:) – 2009-09-19 14:37:43