2012-06-20 49 views
0

我有這種情況發生,每1秒的動作:jQuery。一旦發生這種情況,如何阻止重複行動?

(function(){ 
    $('.testme').jClever(); 
    setTimeout(arguments.callee, 1000); 
})() 

我怎麼能阻止它,一旦它發生了什麼?

更新:.testme來自xmlhttpPost行動,我無法以其他方式工作。

+2

如果你想調用'$( 'TESTME')jClever();'只有一次,你爲什麼包含'setTimeout'? –

+0

你的代碼被封裝在自調用函數中,然後在'setTimeout'內部與函數名稱結合使用,這意味着它將繼續重複,類似於'setInterval',但改進了版本。 – Sarfraz

+0

.testme來自xmlhttpPost操作,我無法以其他方式工作。 –

回答

1

你必須使用clearTimeout()停止的setTimeout(),例如:

var clr = null; 
(function(){ 
    $('.testme').jClever(); 
    clr = setTimeout(arguments.callee, 1000); 
})() 

// assuming that arguments.callee is a function named foo 
function foo() { 
    clearTimeout(clr); 
} 
+1

'arguments.callee'總是指這個函數本身。 –

相關問題