简体   繁体   中英

How to use setInterval() to call a click function periodically within another function?

this is my first programming in Java. I have two functions, #start and #reset. I want to use setInterval to use #reset function within #start after every 10 seconds.

Here are the two functions. First is #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

    $('#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.

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)

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.

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM