[英]How to use setInterval() to call a click function periodically within another function?
這是我在 Java 中的第一次編程。 我有兩個函數,#start 和#reset。 我想使用 setInterval 在每 10 秒后的 #start 內使用 #reset function 。
這是兩個函數。 首先是#start
$('#start').click(function() {
// Calculate the amount of time in milliseconds to run for
var timeleft_s = Math.round(
parseInt($('#hours' ).val())*3600 +
parseInt($('#minutes').val())*60 +
parseInt($('#seconds').val())*1
);
if (timeleft_s > 2147483647) { // Max unsighed 32 bit value
alert("That's too long. Pick a shorter time.");
return;
}
if (APP.last_sent_status !== null && APP.last_sent_status !== APP.status) {
return false;
}
// 0 : not started
// 1 : running
// 2 : stopped
switch(APP.status) {
case 0:
APP.resumeMCA(APP.channel, timeleft_s);
APP.last_sent_status = 1;
break
case 1:
APP.stopMCA(APP.channel);
APP.last_sent_status = 2;
break
case 2:
APP.resumeMCA(APP.channel, timeleft_s);
APP.last_sent_status = 1;
break
default:
break
}
APP.updateButtonStates();
});
這是重置 function
$('#reset').click(function() {
APP.resetHistogram(APP.channel);
});
我想在 start function 中使用 setInterval 確保每 10 秒后執行一次重置 function。
這是我到目前為止沒有運氣的嘗試。 任何幫助表示贊賞:
$('#start').click(function() {
// Calculate the amount of time in milliseconds to run for
var timeleft_s = Math.round(
parseInt($('#hours' ).val())*3600 +
parseInt($('#minutes').val())*60 +
parseInt($('#seconds').val())*1
);
if (timeleft_s > 2147483647) { // Max unsighed 32 bit value
alert("That's too long. Pick a shorter time.");
return;
}
if (APP.last_sent_status !== null && APP.last_sent_status !== APP.status) {
return false;
}
// 0 : not started
// 1 : running
// 2 : stopped
switch(APP.status) {
case 0:
APP.resumeMCA(APP.channel, timeleft_s);
APP.last_sent_status = 1;
break
case 1:
APP.stopMCA(APP.channel);
APP.last_sent_status = 2;
break
case 2:
APP.resumeMCA(APP.channel, timeleft_s);
APP.last_sent_status = 1;
break
default:
break
}
setInterval(#reset,10000)
APP.updateButtonStates();
});
function resetFunction(){
APP.resetHistogram(APP.channel);
}
$('#reset').click(resetFunction);
然后替換
setInterval(#reset,10000)
和setInterval(resetFunction,10000)
由於您將 function 定義為在#reset
單擊時以匿名方式調用,因此沒有可以調用它的參考。
所以你通過給它一個名字來定義它,然后用它代替 onclick function 以及你的 start function
我不完全確定這個答案。 如果您嘗試在#start
onclick
啟動setInterval
,我認為您可以這樣做:
setInterval($("#reset").onclick(),10000);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.