3
我有這樣的代碼JQuery - 如何停止函數?
<script type="text/javascript">
function test(counter) {
if(counter==4)
{
counter=0;
}
if(counter==0)
{
//various stuff
}
counter = counter + 1;
setTimeout(function() { test(counter); }, 7000);
}
$(document).ready(function() {
test(0);
});
</script>
所以當頁面加載test(0)
功能正在運行。
但我有這個鏈接<a onclick="test(1)" rel="2" href="#">
再次調用相同的功能,因此具有相同的功能運行兩次。
有沒有辦法停止正在運行的功能,然後開始新的?
感謝您的回覆!但這將如何幫助我? – mathew 2011-05-19 15:20:05
@mathew:如果你有'setTimeout()'調用,那麼這個函數返回由它設置的定時器的標識符('timerID = setTimeout(...);')。爲了取消掛起的定時器事件,你必須清除它,使用'clearTimeout(timerID);'。 – Orbling 2011-05-19 15:25:10
謝謝你m8! – mathew 2011-05-19 16:04:28