1

I am using this function to auto-click a button after 15 seconds. The problem is the user doesn't leave the page after the option is run and it may be re-run again on the same page but the timer continues. In fact, the timer continues even if I do the action myself.

<script type="text/javascript">
time = 15;
interval = setInterval(function() {
  time--;
    document.getElementById('Label1').innerHTML = "You must choose in " + time + " seconds"
    if (time == 0) {
        // stop timer
        clearInterval(interval);
        // click
        document.getElementById('thebutton').click();       
    }
}, 1000)

</script>

So this script should run the timer and "press" the "thebutton" in fifteen seconds and then the timer should stop counting and reset until run again. If the button is pressed manually before 15 seconds it should still reset.

<input type='submit' id='thebutton' value='Done'></input>

Hopefully this is clear. I am still new and learning.

3
  • what is label 1? Commented Jul 22, 2017 at 3:16
  • It's unclear what you are asking. Maybe you could add some explanation of what you are trying to do. Commented Jul 22, 2017 at 3:19
  • What action causes the timer to start? Commented Jul 22, 2017 at 3:33

5 Answers 5

1

Set a base time and then reset it to that.

<script type="text/javascript">
time = 15;
baseTime = 15;
interval = setInterval(function() {
  time--;
    document.getElementById('Label1').innerHTML = "You must choose in " + time + " seconds"
    if (time == 0) {
        // stop timer
        clearInterval(interval);
        // click
        document.getElementById('thebutton').click(); 
        time = baseTime;
        return false;


    }
}, 1000)

</script>
Sign up to request clarification or add additional context in comments.

2 Comments

This actually half works, this is close. It does reset the time to 15 but the countdown never stops so it resets to 15 but starts counting down again without being re-executed.
I just made an edit. I added return false; That should stop the timer. Let me know if it doesn't.
1

I had a look at the code and the most critical thing that I think you should look at is that the button has no "onclick" function. This means that clicking the button does nothing because you have not put a function there that does something when you click it.

I wrote some code that I hope helps:

let time = 15;
const label = document.getElementById("Label1");
const button = document.getElementById("thebutton");

const getText = () => `You must choose in ${time} seconds`;

const interval = setInterval(() => {
    time--;
    label.innerHTML = getText();
    if (time === 0) {
    // stop timer
    clearInterval(interval);
    // click
    button.click();
  }
}, 1000);

const stopTime = () => {
  clearInterval(interval);
  time = 15;
  label.innerHTML = getText();
};

And in your html something like this:

<input type='submit' id='thebutton' value='Done' onclick="stopTime()" />

Finally I made a small video where I walk through the code, it could be useful as well: https://youtu.be/ZYS9AcxO3d4

Have a great day!

Comments

0

If you only want the button to be clicked once after 15 seconds then you should use the setTimeout() function instead of setInterval().

Then if you do not want the auto-click to happen if the user clicks the button then you would need to add an onClick handler to your button that calls clearTimeout().

Comments

0

I assume you want the label updated as the seconds count down? And it's unclear how the timer is started. Check the below code and see if it does what you expect.

var time, interval;

function stopTimer() {
  if (interval) {
    clearInterval(interval);
    interval = null;
  }
  
  time = 15;
}

function timerAction() {
  $('#lblStatus').text("You must choose in " + time + " seconds");

  if (time-- <= 0) {
    stopTimer();
    console.log("done!");
    $("#btnStop").click();
  }
}

function startTimer() {
  stopTimer();
  timerAction();
  interval = setInterval(timerAction, 1000);
}

$("#btnStart").click(function() {
  startTimer();
});

$("#btnStop").click(function() {
  stopTimer();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span id=lblStatus></span>
<button id='btnStart'>Reset / Start</button>
<button id='btnStop'>Stop</button>

Comments

0

If you want to run only once, you can use setTimeout function

setTimeout(your code, 15000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.