我有一個在頁面加載時執行的函數。該函數使用setTimeout每30秒執行一次。我希望能夠啓用和禁用的setTimeout的onclick我有下面的下面的代碼...如何啓用和禁用setTimeout onclick
<input id="vw" value="" type="hidden">
<a href="#" onclick="document.getElementById('vw').value='0';>click here</a>
<script>
/* JAVASCRIPT BELOW */
function mPb(){
var vw = escape(document.getElementById('vw').value);
if(vw == ''){//DO NOT SWITCH VIEW IF EMPTY
var sTo = setTimeout("mPb()", 30000);
} else {
clearTimeout(sTo);
}
}//END VIEW MY FEED
//ON SERVICE LOAD DISPLAY SHITE INSTEAD OF ONLOAD
window.addEventListener ?
window.addEventListener("load",mPb,false) :
window.attachEvent && window.attachEvent("onload",mPb);
</script>
而問題是......? –
...問題是他在'onclick'結尾缺少一個結束語:'onclick =「document.getElementById('vw')。value ='0';'' – bfavaretto
如果它每30秒執行一次,shouldn你用'setInterval'代替'setTimeout'嗎? – Neil