[英]How to use setInterval() to call a click function periodically within another function?
this is my first programming in Java.这是我在 Java 中的第一次编程。 I have two functions, #start and #reset.
我有两个函数,#start 和#reset。 I want to use setInterval to use #reset function within #start after every 10 seconds.
我想使用 setInterval 在每 10 秒后的 #start 内使用 #reset function 。
Here are the two functions.这是两个函数。 First is #start
首先是#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();
});
Here is the reset function这是重置 function
$('#reset').click(function() {
APP.resetHistogram(APP.channel);
});
I want to use setInterval inside the start function make sure the reset function executes after every 10 seconds.我想在 start function 中使用 setInterval 确保每 10 秒后执行一次重置 function。
Here is what I have tried so far with no luck.这是我到目前为止没有运气的尝试。 Any help is appreciated:
任何帮助表示赞赏:
$('#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);
then replace然后替换
setInterval(#reset,10000)
with setInterval(resetFunction,10000)
setInterval(#reset,10000)
和setInterval(resetFunction,10000)
since you defined the function to be called on #reset
click as anonymous there's no reference as such by which you can call it.由于您将 function 定义为在
#reset
单击时以匿名方式调用,因此没有可以调用它的参考。
so you define it by giving it a name, and then use that in place of onclick function as well as inside your start function所以你通过给它一个名字来定义它,然后用它代替 onclick function 以及你的 start function
I am not totally sure of this answer.我不完全确定这个答案。 If you are trying to start a
setInterval
inside of the onclick
of #start
, here's what I think you could do:如果您尝试在
#start
onclick
启动setInterval
,我认为您可以这样做:
setInterval($("#reset").onclick(),10000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.