【问题标题】:How to use setInterval() to call a click function periodically within another function?如何使用 setInterval() 在另一个函数中定期调用 click 函数?
【发布时间】:2022-01-14 15:56:57
【问题描述】:

这是我第一次使用 Java 编程。我有两个函数,#start 和#reset。我想在每 10 秒后使用 setInterval 在 #start 内使用 #reset 函数。

这是两个函数。首先是#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();
});

这里是重置函数

    $('#reset').click(function() {
    APP.resetHistogram(APP.channel);
});

我想在 start 函数中使用 setInterval 确保每 10 秒后执行 reset 函数。

这是我到目前为止没有运气的尝试。任何帮助表示赞赏:

$('#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();
});

【问题讨论】:

  • 这是javascript。不是java。
  • 另外,setInterval(#reset,10000) 是类上下文之外的非法语法。

标签: javascript setinterval


【解决方案1】:
function resetFunction(){
 APP.resetHistogram(APP.channel);
}

$('#reset').click(resetFunction);

然后替换

setInterval(#reset,10000) 与 setInterval(resetFunction,10000)

由于您将在#reset 上调用的函数定义为匿名单击,因此没有可以调用它的引用。

所以你通过给它一个名字来定义它,然后用它来代替 onclick 函数以及在你的 start 函数中

【讨论】:

  • 不错的答案,很好地解释了这一点。
  • 谢谢你,我的朋友。这就像一个魅力。这实际上是我第一次处理 JavaScript。
【解决方案2】:

我不完全确定这个答案。如果您尝试在#start 的onclick 中启动setInterval,我认为您可以这样做:

 setInterval(() => $("#reset").onclick(),10000);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2018-07-02
    • 2018-12-02
    • 1970-01-01
    • 2016-12-09
    • 2020-03-09
    相关资源
    最近更新 更多