Modify these codes, to auto click a button?

Hi guys,

I found this jquery script.

Can you help me make this codes auto click a button name ‘no’
when it reaches zero seconds?

thanks in advance.

Hi,

You could add the following to the end of the startTimer function:

$("#no").trigger("click");

Do you want the countdown to keep looping, or just countdown once?

I just want countdown once.

Hi pullo

this is what I did so far.

function startTimer(duration, display) {
            var timer = duration, minutes, seconds;
            setInterval(function () {
                minutes = parseInt(timer / 60, 10)
                seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;

                display.text(minutes + ":" + seconds);

                if (--timer < 0) {
                    timer = duration;
                }
            }, 1000);
            
            if(minutes === 0 && seconds === 0) {
                $("#no").trigger("click");
            }
        }

BUt unfortunately it is not working.
Can someone help me please.

thanks.

Hi pullo,

Thank you very much it work properly now.
Just what I need.

Here are the codes,

function startTimer(duration, display) {
            var timer = duration, minutes, seconds;
            setInterval(function () {
                minutes = parseInt(timer / 60, 10)
                seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;

                display.text(minutes + ":" + seconds);

                if (--timer <= 0) {
                    $("#no").trigger("click");
                    
                    timer = duration;
                }
                
            }, 1000);

        }

pullo thanks again…

Hey,

I looked on Stack Overflow (where you got the snippet from) and I much prefer the other solutions listed there.

I modified one of them to better suit your needs:

<div>Registration closes in <span id="time"></span> minutes!</div>

and:

function startTimer(duration, display) {
  var start = Date.now(),
      deferred = $.Deferred(),
      display = document.getElementById(display),
      diff, minutes, seconds, i;

  function timer() {
    diff = duration - (((Date.now() - start) / 1000) | 0);

    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (diff <= 0) {
      clearInterval(i);
      deferred.resolve();
    }
  };

  timer();
  i = setInterval(timer, 1000);

  return deferred.promise();
}

startTimer(10, 'time').then(function(){
  alert("BOOM!");
});

Now the timer only runs once. You can specify any number of seconds as a first parameter without having to hard code that value into the HTML. You specify the id of the container as the second parameter.

The startTime function makes use of jQuery’s deferred object (you seemed to be using jQuery anyway), so that you can specify a function to be called once the countdown is finished (using then()). If you’d like to learn more about this, read: An Introduction to jQuery’s Deferred Objects.

This makes things much easier to read:

startTimer(10, 'time').then(function(){
  alert("BOOM!");
});

Here’s a demo.

Also, this might be of interest: Build a Countdown Timer in Just 18 Lines of JavaScript

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.